1

I have two JSON files named new and old that files have some data. here I want to compare new.json with the old.json file while comparing if I have the same data in those two JSON files I don't want to create any new JSON file

If I have different data like below in new.json and old.json

new.json:

[
 {
    "name": "Mohan raj",
    "age": 23,
    "country": "INDIA"
 },
 {
    "name": "Kiruthika",
    "age": 18,
    "country": "INDIA"
 },
 {
    "name": "Munusamy",
    "age": 45,
    "country": "INDIA"
 },
 {
    "name": "John Wood",
    "age": 35,
    "country": "USA"
 },
 {
    "name": "Mark Smith",
    "age": 25,
    "country": "USA"
 }
]

old.json:

[
 {
    "name": "John Wood",
    "age": 35,
    "country": "USA"
 },
 {
    "name": "Mark Smith",
    "age": 30,
    "country": "USA"
 },
 {
    "name": "Oscar Bernard",
    "age": 25,
    "country": "Australia"
 }
]

If the new.json file has any of the same data of old.json having we have to skip that data and the new.json file have any of the updated data of old.json having and the new data's in new.json we have to create a new JSON file named updated.json with the data of the above scenarios.

The resulted JSON file needs to look like this:

updated.json:

[
 {
    "name": "Mohan raj",
    "age": 23,
    "country": "INDIA"
 },
 {
    "name": "Kiruthika",
    "age": 18,
    "country": "INDIA"
 },
 {
    "name": "Munusamy",
    "age": 45,
    "country": "INDIA"
 },
 {
    "name": "Mark Smith",
    "age": 25,
    "country": "USA"
 }
]
Zach Young
  • 10,137
  • 4
  • 32
  • 53

2 Answers2

0

You can achieve it below. keep track of data from old json.

import json

# read json file
with open('new.json') as f:
    new_data = json.load(f)
with open('old.json') as f:
    old_data = json.load(f)

old_json_list = [
    {elem["name"], elem["age"], elem["country"]} for elem in old_data]
    
updated_list = []
for elem in new_data:
    elm = {elem["name"], elem["age"], elem["country"]}
    if elm not in old_json_list:
        updated_list.append(elem)

with open('updated.json', 'w') as f:
    json.dump(updated_list, f)
shivankgtm
  • 1,207
  • 1
  • 9
  • 20
0

Took me a while to get, thanks for answering my questions, and it seems like "updated" might simply be expressed as "new not in old"?

I think so, because the following seems to do the job.

The key is to make comparisons of the objects themselves, and not wanting to get into object comparison (deep-equal), just hashing each object back to JSON gives us string representations we can compare:

import json

old_hashes = []

old_objs = json.load(open('old.json'))
for old_obj in old_objs:
    old_hash = json.dumps(old_obj)
    old_hashes.append(old_hash)


# "Updated" means "new not in old"
updated_objs = []

new_objs = json.load(open('new.json'))
for new_obj in new_objs:
    new_hash = json.dumps(new_obj)
    if new_hash not in old_hashes:
        updated_objs.append(new_obj)


print(json.dumps(updated_objs, indent=2))

When I run that against your old.json and new.json, I get:

[
  {
    "name": "Mohan raj",
    "age": 23,
    "country": "INDIA"
  },
  {
    "name": "Kiruthika",
    "age": 18,
    "country": "INDIA"
  },
  {
    "name": "Munusamy",
    "age": 45,
    "country": "INDIA"
  },
  {
    "name": "Mark Smith",
    "age": 25,
    "country": "USA"
  }
]
Zach Young
  • 10,137
  • 4
  • 32
  • 53
  • I also need to write this as a new file –  Jan 22 '22 at 21:02
  • Does the algorithm work? Do I understand the problem and did I present a _workable_ solution? You can check out the docs on reading and [writing](https://docs.python.org/3/library/json.html#json.dump) JSON. – Zach Young Jan 22 '22 at 21:05
  • No, I cannot; and that's not why I answered this question. I'm trying to _help you_ solve a problem, not write complete coded solutions for you. That's not what StackOverflow is for. – Zach Young Jan 22 '22 at 21:08
  • The Algorithm is working but if this is in OOP Structure that will be useful for me very much.. –  Jan 22 '22 at 21:08
  • Then you should accept this answer, because it satisfies the question you originally asked. – Zach Young Jan 22 '22 at 21:09