Skip to main content
edited title
Link

How to correctly merge 2 JSON files including objects and arrays using jq?

added 1 character in body
Source Link

Best result I've got so far (only arrayarrays with actors are missing):

Best result I've got so far (only array with actors are missing):

Best result I've got so far (only arrays with actors are missing):

added 105 characters in body; edited title
Source Link
{ "series": "Harry Potter Movie Series", "producent""producer": "David Heyman", "movies": [ { "title": "Harry Potter and the Philosopher's Stone", "year": "2001" }, { "title": "Harry Potter and the Chamber of Secrets", "year": "2002" } ] } 
{ "series": "Harry Potter Movie Series", "writer": "J.K. Rowling", "movies": [ { "title": "Harry Potter and the Philosopher's Stone", "year": "2001", "actors": [ { "names": [ "Emma Watson", "Other actor" ], "other": "Some value" } ] }, { "title": "Harry Potter and the Chamber of Secrets", "year": "2001", "actors": [ { "names": [ "Emma Watson" ], "other": "Some value" } ] } ], "producent""producer": "David Heyman" } 
{ "series": "Harry Potter Movie Series", "writer": "J.K. Rowling", "movies": [ { "title": "Harry Potter and the Philosopher's Stone", "year": "2001" }, { "title": "Harry Potter and the Chamber of Secrets", "year": "2002" } ], "producent""producer": "David Heyman" } 
 

If I switch order of files I either end up losing 'actors' from file1 or 'year' from file2.

How it should work:

  • the elements in file 2 will be leading and should replace the matching elements in file 1.
  • the elements in file 1 that doesn't exist in file 2 (like writer and movies[].actors elements) shouldn't be deleted
  • the elements in file 2 that doesn't exist yet in file 1 will be added (like producer and movies[].year).
  • a title is unique and should by default not occur more then once, but if it does remove the duplicates.
{ "series": "Harry Potter Movie Series", "producent": "David Heyman", "movies": [ { "title": "Harry Potter and the Philosopher's Stone", "year": "2001" }, { "title": "Harry Potter and the Chamber of Secrets", "year": "2002" } ] } 
{ "series": "Harry Potter Movie Series", "writer": "J.K. Rowling", "movies": [ { "title": "Harry Potter and the Philosopher's Stone", "year": "2001", "actors": [ { "names": [ "Emma Watson", "Other actor" ], "other": "Some value" } ] }, { "title": "Harry Potter and the Chamber of Secrets", "year": "2001", "actors": [ { "names": [ "Emma Watson" ], "other": "Some value" } ] } ], "producent": "David Heyman" } 
{ "series": "Harry Potter Movie Series", "writer": "J.K. Rowling", "movies": [ { "title": "Harry Potter and the Philosopher's Stone", "year": "2001" }, { "title": "Harry Potter and the Chamber of Secrets", "year": "2002" } ], "producent": "David Heyman" } 

If I switch order of files I either end up losing 'actors' from file1 or 'year' from file2.

{ "series": "Harry Potter Movie Series", "producer": "David Heyman", "movies": [ { "title": "Harry Potter and the Philosopher's Stone", "year": "2001" }, { "title": "Harry Potter and the Chamber of Secrets", "year": "2002" } ] } 
{ "series": "Harry Potter Movie Series", "writer": "J.K. Rowling", "movies": [ { "title": "Harry Potter and the Philosopher's Stone", "year": "2001", "actors": [ { "names": [ "Emma Watson", "Other actor" ], "other": "Some value" } ] }, { "title": "Harry Potter and the Chamber of Secrets", "year": "2001", "actors": [ { "names": [ "Emma Watson" ], "other": "Some value" } ] } ], "producer": "David Heyman" } 
{ "series": "Harry Potter Movie Series", "writer": "J.K. Rowling", "movies": [ { "title": "Harry Potter and the Philosopher's Stone", "year": "2001" }, { "title": "Harry Potter and the Chamber of Secrets", "year": "2002" } ], "producer": "David Heyman" } 
 

If I switch order of files I either end up losing 'actors' from file1 or 'year' from file2.

How it should work:

  • the elements in file 2 will be leading and should replace the matching elements in file 1.
  • the elements in file 1 that doesn't exist in file 2 (like writer and movies[].actors elements) shouldn't be deleted
  • the elements in file 2 that doesn't exist yet in file 1 will be added (like producer and movies[].year).
  • a title is unique and should by default not occur more then once, but if it does remove the duplicates.
added 105 characters in body; edited title
Source Link
Loading
added 141 characters in body
Source Link
Loading
deleted 3 characters in body
Source Link
Loading
Source Link
Loading