0

Say I have two versions of the same JSON data. Each object has a unique ID key/pair. Say in one version a certain key has null as its value while the other version has the appropriate values for that key.

Can we match the objects between the two JSON files based on their unique ID value and copy over a certain key's values? I don't want to mess around with the rest of the fields in the JSON object.

Sample:

version 1:

[
  {
    "contentId": "ID-02",
    "title": "Attendance",
    "desp": "Daily Attendance",
    "contentType": "service",
    "url": "ATTENDANCE",
    "contentCategory": "Essentials",
    "employeeId": null,
    "imageUrl": null,
    "publishedCourseFlag": "true"
  },
  {
    "contentId": "ID-04",
    "title": "Regularise History",
    "desp": "Regularise History",
    "contentType": "service",
    "url": "REGULARISE_HISTORY",
    "contentCategory": "Non-Essentials",
    "employeeId": null,
    "imageUrl": null,
    "publishedCourseFlag": "false"
  }
]

version 2:

[
  {
    "contentId": "ID-02",
    "title": "Attendance",
    "desp": "Daily Attendance",
    "contentType": "service",
    "url": "ATTENDANCE",
    "contentCategory": null,
    "employeeId": null,
    "imageUrl": null
  },
  {
    "contentId": "ID-04",
    "title": "Regularise History",
    "desp": "Regularise History",
    "contentType": "service",
    "url": "REGULARISE_HISTORY",
    "contentCategory": null,
    "employeeId": null,
    "imageUrl": null,
    "publishedCourseFlag": "false"
  }
]

Here v1 has the contentCategory set while v2 has null. I want to copy the appropriate values from v1 to v2 based on their contentId values. Is there a simple way to do this other than manually copy/paste? I'd like a scripting solution that I can modify to my needs for situations like this. Or a simple query like solution would be even better.

Additionally is it possible to add a certain missing key/value from version 1 to version 2 ? Like Update if present else Insert ? I am hoping for a lot I guess. Completely new to JSON.

BharathYes
  • 787
  • 2
  • 9
  • 25
  • Which language/library are you using? – Jack Fleeting Feb 14 '20 at 16:35
  • I am looking at python since I've heard it is simpler to script with. Else I will try with java as I have previously parsed json string into list of maps. But running a Java code sounds too big for this purpose. In python I've seen a library named 'json' but still not sure of how to use it. – BharathYes Feb 14 '20 at 17:14
  • I was more interested in knowing if any tools are present, say with a GUI, that allows json editing or better a query like language, like with SQL, since json is a structured data format. This question was more about learning the possibilities of json manipulation than to get a script for this particular case. – BharathYes Feb 14 '20 at 17:16
  • Yes, it's definitely doable in python. Just search around and you'll find enough information about python generally, and json in python specifically, to get you started. – Jack Fleeting Feb 14 '20 at 19:18

0 Answers0