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

3

2 Answers 2

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());
Sign up to request clarification or add additional context in comments.

2 Comments

can not use jquery.., tried your native js answer here..am I doing something wrong..jsfiddle.net/kbvbrrjr/1
Fixed it, take a look now here: jsfiddle.net/kbvbrrjr/6 Also, updated the answer.
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()));

1 Comment

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 =

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.