1

I have two json object and i want to merge it. it means ifsame key is there then want to overwrite it. How I can do it using node.js. kindly check below sample:

first object

{
"title": "Test",
"url": "/test",
"gf": {
    "name": "kim",
    "last": "john"
},
"created_at": "2021-09-08T18:40:50.152Z",
"updated_at": "2021-09-08T18:54:36.387Z",
"version": 9
}

Second Object

{
"gf": {
    "last": "Anup"
},
"__originalParams": {
    "gf": {
        "last": "Anup"
    }
}
}

Required Result

{
"title": "Test",
"url": "/test",
"gf": {
    "name": "kim",
    "last": "Anup"
},
"created_at": "2021-09-08T18:40:50.152Z",
"updated_at": "2021-09-08T18:54:36.387Z",
"version": 9,
"__originalParams": {
    "gf": {
        "last": "Anup"
    }
}
}

How I get this result using node.js . It is just a sample I have complex JSON structure too. is any direct option present in Lodash or Ramda for this. Kindly help me here

Suraj Dalvi
  • 988
  • 1
  • 20
  • 34
  • Does this answer your question? [How to deep merge instead of shallow merge?](https://stackoverflow.com/questions/27936772/how-to-deep-merge-instead-of-shallow-merge) – num8er Sep 08 '21 at 19:35

3 Answers3

1

You can use the JavaScript's Spread operator for this purpose. You can try,

let obj1 = {
    key: 'value'
    ...
}
let obj2 = {
    key: 'value'
    ...
}
console.log({ ...obj1, ...obj2 })

You will get the desired output by replacing the values of obj 1 by values of obj2

Vishnu Vinod
  • 603
  • 4
  • 15
1

You can use Object.assign:

let obj1 = {
"gf": {
    "last": "Anup"
},
"__originalParams": {
    "gf": {
        "last": "Anup"
    }
}
};

let obj2 = {
"title": "Test",
"url": "/test",
"gf": {
    "name": "kim",
    "last": "john"
},
"created_at": "2021-09-08T18:40:50.152Z",
"updated_at": "2021-09-08T18:54:36.387Z",
"version": 9
};

let merged = Object.assign({}, obj1, obj2);

console.log(merged)
LeeLenalee
  • 27,463
  • 6
  • 45
  • 69
  • https://www.samanthaming.com/tidbits/70-3-ways-to-clone-objects/#performance, https://www.measurethat.net/Benchmarks/Show/2136/0/javascript-spread-operator-vs-objectassign-performance, https://gist.github.com/syaau/e9b991e55aed8c249fa5a86511f77ca5, https://www.measurethat.net/Benchmarks/Show/9596/0/javascript-spread-operator-vs-objectassign-performance – LeeLenalee Sep 08 '21 at 19:42
  • This answer doesn't give the desired result. The OP's required result indicates `gf.last` on first object ("john") should be replaced by the second object's `gf.last` property ("Anup") so that result `gf.last` should be "Anup", not "john" – Timur Sep 08 '21 at 20:18
1

You can use lodash merge or the deepmerge package:

var lhs = {
  "title": "Test",
  "url": "/test",
  "gf": {
    "name": "kim",
    "last": "john"
  },
  "created_at": "2021-09-08T18:40:50.152Z",
  "updated_at": "2021-09-08T18:54:36.387Z",
  "version": 9
};

var rhs = {
  "gf": {
    "last": "Anup"
  },
  "__originalParams": {
    "gf": {
      "last": "Anup"
    }
  }
}

var ans = deepmerge(lhs, rhs)
var ans2 = _.merge(lhs, rhs)

console.log(ans)
console.log(ans2)
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
<script src="https://unpkg.com/deepmerge@4.2.2/dist/umd.js"></script>
Timur
  • 1,682
  • 1
  • 4
  • 11