menu

Questions & Answers

How to map two JSON objects of different formats?

Hello again great minds of stack overflow! Iam working in an angular project and my question is about mapping JSON objects. So i have JSON Object that i am taking out from html it is of the format

const obj = {
  firstLevelKey1 : "Value1",
  thirdLevelKey2 : [{
    "diffKey1": "diffValue1",
    "diffKey2" : "diffValue2",
    "diffKey3" : "diffValue3"
  },{
    "diffKey1": "diffValue1",
    "diffKey2" : "diffValue2",
    "diffKey3" : "diffValue3"
  },{
    "diffKey1": "diffValue1",
    "diffKey2" : "diffValue2",
    "diffKey3" : "diffValue3"
  }],
  thirdLevelKey3 : ["string1", "string2", "string3"]
}

the format I want to map with is like this

const objToMap = [{
  firstLevelKey1 : [{
    dataType : 'String'
  },{
    value : 'someString'
  },{
    someKey : 'someValue'
  }]
},{
  firstLevelKey2:[{
    dataType : 'Object'
  },{
    value : [{
      secondLevelKey1 :[{
        dataType : 'String'
      },{
        value : 'someString'
      },{
        someKey : 'someValue'
      }]
    },{
      secondLevelKey2 : [{
        dataType : 'Object'
      },{
        value : [{
          thirdLevelKey1:[{
            dataType : 'String'
          },{
            value : 'someString'
          },{
            someKey : 'someValue'
          }]
        },{
          thirdLevelKey2 : [{
            dataType : 'String'
          },{
            value : 'someString'
          },{
            someKey : 'someValue'
          }]
        },{
          thirdLevelKey3 : [{
            dataType : 'String'
          },{
            value : 'someString'
          },{
            someKey : 'someValue'
          }]
        }]
      },{
        someKey : 'someValue'
      }]
    },{
      secondLevelKey3 :[{
        dataType : 'String'
      },{
        value : 'someString'
      },{
        someKey : 'someValue'
      }]
    }]
  },{
    someKey : 'someValue'
  }]
},{
  firstLevelKey3 : [{
    dataType : 'String'
  },{
    value : 'someString'
  },{
    someKey : 'someValue'
  }]
}]

after getting the flattening with keyNames function from here, the great stack overflow the json object I can also generate (using innertext property)

const anotherObj = {
  "firstLevelKey1" : "firstLevelValue",
  "firstLevelKey2#secondLevlKey1" : "secondLevelValue1",
  "firstLevelKey2#secondLevelKey2#thirdLevelKey1" : "thirdLevelValue1",
  "firstLevelKey2#secondLevelKey2#thirdLevelKey2" : [{
    "diffKey1": "diffValue1",
    "diffKey2" : "diffValue2",
    "diffKey3" : "diffValue3"
  },{
    "diffKey1": "diffValue1",
    "diffKey2" : "diffValue2",
    "diffKey3" : "diffValue3"
  },{
    "diffKey1": "diffValue1",
    "diffKey2" : "diffValue2",
    "diffKey3" : "diffValue3"
  }],
  "firstLevelKey2#secondLevelKey2#thirdLevelKey3": ["string1", "string2", "string3"],
  "firstLevelKey2#secondLevelKey3" : "secondLevelValue3",
  "firstLevelKey3" : "firstLevelValue3"
}

so can I please know how do I map obj/anotherObj to objToMap, the values from obj/anotherObj should map for value key in objToMap, that is I should send it back in the same format I got in with values added in any help is greatly appreciated.

Answers(1) :

Use a library - Lodash: use _.merge() which can be used to merge two JSON objects. Copy code

let mergedJson = _.merge(json1, json2);