Home » Javascript » Create nested JSON from row type object's array in javascript/jquery

Create nested JSON from row type object's array in javascript/jquery

Posted by: admin August 19, 2018 Leave a comment

Questions:

I have to create nested json from objects of array type row wise, and parent of every children not repeated see. the same converted hard coded json structure, i want the same structure with java-script code…please help

source row objects array:

[{
  standard: 'JEE',
  subject: 'Physics',
  chapter: 'Chapter 1',
  topic: 'Topic 1.1',
  lvl: 'Easy',
  tm: 'In time',
  question: 'Question 1',
  answer: 'correct',
  size: 3938
}, {
  standard: 'JEE',
  subject: 'Physics',
  chapter: 'Chapter 1',
  topic: 'Topic 1.1',
  lvl: 'Medium',
  tm: 'In time',
  question: 'Question 2',
  answer: 'correct',
  size: 3938
}, {
  standard: 'JEE',
  subject: 'Physics',
  chapter: 'Chapter 1',
  topic: 'Topic 1.1',
  lvl: 'Medium',
  tm: 'Less time',
  question: 'Question 3',
  answer: 'incorrect',
  size: 3938
}, {
  standard: 'JEE',
  subject: 'Physics',
  chapter: 'Chapter 1',
  topic: 'Topic 1.1',
  lvl: 'Difficult',
  tm: 'In time',
  question: 'Question 4',
  answer: 'skipped',
  size: 3938
}, {
  standard: 'JEE',
  subject: 'Physics',
  chapter: 'Chapter 1',
  topic: 'Topic 1.2',
  lvl: 'Easy',
  tm: 'In time',
  question: 'Question 5',
  answer: 'skipped',
  size: 3938
}, {
  standard: 'JEE',
  subject: 'Physics',
  chapter: 'Chapter 1',
  topic: 'Topic 1.2',
  lvl: 'Easy',
  tm: 'Less time',
  question: 'Question 6',
  answer: 'incorrect',
  size: 3938
}, {
  standard: 'JEE',
  subject: 'Physics',
  chapter: 'Chapter 1',
  topic: 'Topic 1.2',
  lvl: 'Medium',
  tm: 'In time',
  question: 'Question 7',
  answer: 'correct',
  size: 3938
}, {
  standard: 'JEE',
  subject: 'Physics',
  chapter: 'Chapter 1',
  topic: 'Topic 1.2',
  lvl: 'Difficult',
  tm: 'Less time',
  question: 'Question 8',
  answer: 'skipped',
  size: 3938
}, {
  standard: 'JEE',
  subject: 'Physics',
  chapter: 'Chapter 2',
  topic: 'Topic 2.1',
  lvl: 'Easy',
  tm: 'In time',
  question: 'Question 9',
  answer: 'incorrect',
  size: 3938
}, {
  standard: 'JEE',
  subject: 'Physics',
  chapter: 'Chapter 2',
  topic: 'Topic 2.1',
  lvl: 'Easy',
  tm: 'Less time',
  question: 'Question 10',
  answer: 'incorrect',
  size: 3938
}, {
  standard: 'JEE',
  subject: 'Physics',
  chapter: 'Chapter 2',
  topic: 'Topic 2.1',
  lvl: 'Medium',
  tm: 'In time',
  question: 'Question 11',
  answer: 'correct',
  size: 3938
}, {
  standard: 'JEE',
  subject: 'Physics',
  chapter: 'Chapter 2',
  topic: 'Topic 2.1',
  lvl: 'Difficult',
  tm: 'Less time',
  question: 'Question 12',
  answer: 'skipped',
  size: 3938
}, {
  standard: 'JEE',
  subject: 'Chemistry',
  chapter: 'Chapter 1',
  topic: 'Topic 1.1',
  lvl: 'Easy',
  tm: 'In time',
  question: 'Question 13',
  answer: 'correct',
  size: 3938
}, {
  standard: 'JEE',
  subject: 'Chemistry',
  chapter: 'Chapter 1',
  topic: 'Topic 1.1',
  lvl: 'Medium',
  tm: 'In time',
  question: 'Question 14',
  answer: 'incorrect',
  size: 3938
}, {
  standard: 'JEE',
  subject: 'Chemistry',
  chapter: 'Chapter 1',
  topic: 'Topic 1.1',
  lvl: 'Difficult',
  tm: 'Less time',
  question: 'Question 15',
  answer: 'correct',
  size: 3938
}, {
  standard: 'JEE',
  subject: 'Chemistry',
  chapter: 'Chapter 2',
  topic: 'Topic 2.1',
  lvl: 'Easy',
  tm: 'In time',
  question: 'Question 16',
  answer: 'incorrect',
  size: 3938
}, {
  standard: 'JEE',
  subject: 'Chemistry',
  chapter: 'Chapter 2',
  topic: 'Topic 2.1',
  lvl: 'Medium',
  tm: 'In time',
  question: 'Question 17',
  answer: 'skipped',
  size: 3938
}, {
  standard: 'JEE',
  subject: 'Chemistry',
  chapter: 'Chapter 2',
  topic: 'Topic 2.1',
  lvl: 'Difficult',
  tm: 'In time',
  question: 'Question 18',
  answer: 'correct',
  size: 3938
}]

I want to make a nested structure from objects of arrays following info below:

    {
  "name": "JEE",
  "type": "std",
  "children": [{
    "name": "Physics",
    "type": "subject",
    "children": [{
      "name": "Chapter 1",
      "type": "chapter",
      "children": [{
        "name": "Topic 1.1",
        "type": "topc",
        "children": [{
          "name": "Easy",
          "type": "lvl",
          "children": [{
            "name": "In Time",
            "type": "tm",
            "children": [{
              "name": "Question 1",
              "answer": "correct",
              "type": "question",
              "size": 3938
            }]
          }]
        }, {
          "name": "Medium",
          "type": "lvl",
          "children": [{
            "name": "In time",
            "type": "tm",
            "children": [{
              "name": "Question 2",
              "answer": "correct",
              "type": "question",
              "size": 3938
            }]
          }, {
            "name": "Less time",
            "type": "tm",
            "children": [{
              "name": "Question 3",
              "answer": "incorrect",
              "type": "question",
              "size": 3938
            }]
          }]
        }, {
          "name": "Difficult",
          "type": "lvl",
          "children": [{
            "name": "In time",
            "type": "tm",
            "children": [{
              "name": "Question 4",
              "answer": "skipped",
              "type": "question",
              "size": 3938
            }]
          }]
        }]
      }, {
        "name": "Topic 1.2",
        "type": "topc",
        "children": [{
          "name": "Easy",
          "type": "lvl",
          "children": [{
            "name": "In Time",
            "type": "tm",
            "children": [{
              "name": "Question 5",
              "answer": "skipped",
              "type": "question",
              "size": 3938
            }]
          }, {
            "name": "Less Time",
            "type": "tm",
            "children": [{
              "name": "Question 6",
              "answer": "incorrect",
              "type": "question",
              "size": 3938
            }]
          }]
        }, {
          "name": "Medium",
          "type": "lvl",
          "children": [{
            "name": "In time",
            "type": "tm",
            "children": [{
              "name": "Question 7",
              "answer": "correct",
              "type": "question",
              "size": 3938
            }]
          }]
        }, {
          "name": "Difficult",
          "type": "lvl",
          "children": [{
            "name": "Less time",
            "type": "tm",
            "children": [{
              "name": "Question 8",
              "answer": "skipped",
              "type": "question",
              "size": 3938
            }]
          }]
        }]
      }]
    }, {
      "name": "Chapter 2",
      "type": "chapter",
      "children": [{
        "name": "Topic 2.1",
        "type": "topc",
        "children": [{
          "name": "Easy",
          "type": "lvl",
          "children": [{
            "name": "In Time",
            "type": "tm",
            "children": [{
              "name": "Question 9",
              "answer": "incorrect",
              "type": "question",
              "size": 3938
            }]
          }, {
            "name": "Less Time",
            "type": "tm",
            "children": [{
              "name": "Question 10",
              "answer": "incorrect",
              "type": "question",
              "size": 3938
            }]
          }]
        }, {
          "name": "Medium",
          "type": "lvl",
          "children": [{
            "name": "In time",
            "type": "tm",
            "children": [{
              "name": "Question 11",
              "answer": "correct",
              "type": "question",
              "size": 3938
            }]
          }]
        }, {
          "name": "Difficult",
          "type": "lvl",
          "children": [{
            "name": "Less time",
            "type": "tm",
            "children": [{
              "name": "Question 12",
              "answer": "skipped",
              "type": "question",
              "size": 3938
            }]
          }]
        }]
      }]
    }]
  }, {
    "name": "Chemistry",
    "type": "subject",
    "children": [{
      "name": "Chapter 1",
      "type": "chapter",
      "children": [{
        "name": "Topic 1.1",
        "type": "topc",
        "children": [{
          "name": "Easy",
          "type": "lvl",
          "children": [{
            "name": "In Time",
            "type": "tm",
            "children": [{
              "name": "Question 13",
              "answer": "correct",
              "type": "question",
              "size": 3938
            }]
          }]
        }, {
          "name": "Medium",
          "type": "lvl",
          "children": [{
            "name": "In time",
            "type": "tm",
            "children": [{
              "name": "Question 14",
              "answer": "incorrect",
              "type": "question",
              "size": 3938
            }]
          }]
        }, {
          "name": "Difficult",
          "type": "lvl",
          "children": [{
            "name": "Less time",
            "type": "tm",
            "children": [{
              "name": "Question 15",
              "answer": "correct",
              "type": "question",
              "size": 3938
            }]
          }]
        }]
      }]
    }, {
      "name": "Chapter 2",
      "type": "chapter",
      "children": [{
        "name": "Topic 2.1",
        "type": "topc",
        "children": [{
          "name": "Easy",
          "type": "lvl",
          "children": [{
            "name": "In Time",
            "type": "tm",
            "children": [{
              "name": "Question 16",
              "answer": "incorrect",
              "type": "question",
              "size": 3938
            }]
          }]
        }, {
          "name": "Medium",
          "type": "lvl",
          "children": [{
            "name": "In time",
            "type": "tm",
            "children": [{
              "name": "Question 17",
              "answer": "skipped",
              "type": "question",
              "size": 3938
            }]
          }]
        }, {
          "name": "Difficult",
          "type": "lvl",
          "children": [{
            "name": "In time",
            "type": "tm",
            "children": [{
              "name": "Question 18",
              "answer": "correct",
              "type": "question",
              "size": 3938
            }]
          }]
        }]
      }]
    }]
  }]
}
Answers: