0

I have JSON object as below, i am trying to group and push this object by first set of keys.

main =  {"answer_options":{
            "1":{"1":"optical projection<\/p>\r\n","2":"optical mechanism projection<\/p>\r\n","3":"mechanical projection<\/p>\r\n","4":"all the above<\/p>\r\n"},
            "2":{"5":"Greenwich to the place<\/p>\r\n","6":"equator to the poles<\/p>\r\n","7":"equator to the nearer pole<\/p>\r\n","8":"equator to the nearer pole along the meridian of the place<\/p>\r\n","9":"none of these.<\/p>\r\n"}},
    "question":
            {"1":"The stereo plotting instruments are generally manufactured on the principle of<\/p>\r\n","2":"Latitude of a place is the angular distance from<\/p>\r\n"}
    };

I trying to get the output as below.. Can any one help on this.

{"1" : 
        {{"1" : "The stereo plotting instruments are generally manufactured on the principle of<\/p>\r\n"},
        {"1":"optical projection<\/p>\r\n","2":"optical mechanism projection<\/p>\r\n","3":"mechanical projection<\/p>\r\n","4":"all the above<\/p>\r\n"}}, 
    {"2": 
        {{"2" : "Latitude of a place is the angular distance from<\/p>\r\n"},
        {"5":"Greenwich to the place<\/p>\r\n","6":"equator to the poles<\/p>\r\n","7":"equator to the nearer pole<\/p>\r\n","8":"equator to the nearer pole along the meridian of the place<\/p>\r\n","9":"none of these.<\/p>\r\n"}}
    }
5
  • lets see what you have done, so far Commented Feb 13, 2018 at 9:23
  • Yeah, the output should be in {}. Commented Feb 13, 2018 at 9:27
  • 3
    {{ is not valid JSON. Everything inside {} must be "key":value Commented Feb 13, 2018 at 9:28
  • Are you sure you don't mean {"question": { "1": "The stereo..."}, "answer_options": {"1": "optical projection", "2": "optical mechanism", ...}} Commented Feb 13, 2018 at 9:32
  • each question having key as unique id and answer option having this id as key (set of answer options for each question). I want to get this in a optimized format. If question id and answer set id same then push question and answer set in one object. Commented Feb 13, 2018 at 9:36

1 Answer 1

1

Your expected out is not really a advisable (or commonly used) format. This is an alternative approach. You can use for / in to loop thru questions and match it to the corresponding answer options.

main = {
  "answer_options": {
    "1": {
      "1": "optical projection<\/p>\r\n",
      "2": "optical mechanism projection<\/p>\r\n",
      "3": "mechanical projection<\/p>\r\n",
      "4": "all the above<\/p>\r\n"
    },
    "2": {
      "5": "Greenwich to the place<\/p>\r\n",
      "6": "equator to the poles<\/p>\r\n",
      "7": "equator to the nearer pole<\/p>\r\n",
      "8": "equator to the nearer pole along the meridian of the place<\/p>\r\n",
      "9": "none of these.<\/p>\r\n"
    }
  },
  "question": {
    "1": "The stereo plotting instruments are generally manufactured on the principle of<\/p>\r\n",
    "2": "Latitude of a place is the angular distance from<\/p>\r\n"
  }
};

var newMain = {};
for (var key in main.question) {
  newMain[ key ] = [
      main.question[key],
      main.answer_options[key]
    ];
}

console.log(newMain);

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, this what i am looking for.
Great! Happy to help @PrudhviMallavarapu

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.