0

I have a complex javascript code which when simplified is as below..

function getjson1() {
    return {
                'json1': {
                    id: 'jsonid1'
                }
            };
        }

function getjson2() {
    return {
                'json2': {
                    id: 'jsonid2'
                }
            };
        }

myjson = [];

myjson.push(getjson1());

myjson.push(getjson2());

function finaljson() {
    return {
                'json': myjson
            };
        }

console.log(JSON.stringify(finaljson()));

Now the result of this code is

{"json":[{"json1":{"id":"jsonid1"}},{"json2":{"id":"jsonid2"}}]}

Now this code I need to change such that I can get rid of the array and can traverse the json object like.. json.json1.id, etc..

One example could be as below..

{"json":{"json1":{"id":"jsonid1"},"json2":{"id":"jsonid2"}}}

Any help is sincerely appreciated.

Thanks

baao
  • 71,625
  • 17
  • 143
  • 203
Arnab
  • 2,324
  • 6
  • 36
  • 60

2 Answers2

2

Well if you don't want an array, don't use one. First, a jQuery-based solution:

myjson = {};
myjson = $.extend(myjson, getjson1());
myjson = $.extend(myjson, getjson2());

In native JavaScript, you can use the following function:

function extend (target, source) {
    Object.keys(source).map(function (prop) {
        target[prop] = source[prop];
    });
    return target;
};

This way, the first code becomes this:

myjson = {};
myjson = extend(myjson, getjson1());
myjson = extend(myjson, getjson2());
meskobalazs
  • 15,741
  • 2
  • 40
  • 63
  • can not use jquery.., tried your native js answer here..am I doing something wrong..http://jsfiddle.net/kbvbrrjr/1/ – Arnab Aug 03 '15 at 10:50
  • 1
    Fixed it, take a look now here: http://jsfiddle.net/kbvbrrjr/6/ Also, updated the answer. – meskobalazs Aug 03 '15 at 11:06
1

You are pushing it to an array so you are getting an array.

use this simple add function to push it in an object in the format you want.

First key in the function returns will be the key in the end object.

function getjson1() {
    return {
                'json1': {
                    id: 'jsonid1'
                }
            };
        }

function getjson2() {
    return {
                'json2': {
                    id: 'jsonid2'
                }
            };
        }


function add(obj, toadd) {
   for(var key in toadd) {
      if(toadd.hasOwnProperty(key)) {
          obj[key] = toadd[key];
          break;
      }
   }
   return obj;
}
myjson = {};
add(myjson,getjson1());
add(myjson,getjson2());

function finaljson() {
    return {
                'json': myjson
            };
        }

console.log(JSON.stringify(finaljson()));
Tschallacka
  • 27,901
  • 14
  • 88
  • 133
  • 1
    http://jsfiddle.net/mdibbets/kbvbrrjr/5/ I forgot to implement a return statement. You made `myjson =` but that isnt needed. I added a return statement to catch both cases. The add function modifies the passed obj directly. Now it will work for both cases. With and without an `var =` – Tschallacka Aug 03 '15 at 10:55