-2

I would like to merge 2 JSON files

tests.json with empty value, some of them - nested

{
  "tests": [{
    "id": 1,
    "value": "",
    "values": "info..."
  }, {
    "id": 41,
    "title": "Debug test",
    "value": "",
    "values": [{
      "id": 345,
      "value": "",
      "values": [ {
        "id": 230,
        "values": [{
          "id": 234,
          "value": ""
        }, {
          "id": 653,
          "value": ""
        }]
      }]
    }],
  }, {...

values.json with the value

{
  "values": [{
    "id": 2,
    "value": "passed"
  }, {
    "id": 41,
    "value": "passed"
  }, {
    "id": 345,
    "value": "passed"
  }, {
    "id": 230,
    "value": "passed"
  },{
    "id": 234,
    "value": "passed"
  },{
    "id": 653,
    "value": "passed"
  },{...

This code works fine, but I need to make it more compatible

import json

with open("tests.json") as fo:
    data1 = json.load(fo)
with open("values.json") as fo:
    data2 = json.load(fo)

for dest in data1['tests']:

    if 'values' in dest:
        for dest_1 in dest['values']:
            if 'values' in dest_1:
                for dest_2 in dest_1['values']:
                    if 'values' in dest_2:
                        for dest_3 in dest_2['values']:

                            for source in data2['values']:
                                if source['id'] == dest_3['id']:
                                    dest_3['value'] = source['value']

                    for source in data2['values']:
                        if source['id'] == dest_2['id']:
                            dest_2['value'] = source['value']

            for source in data2['values']:
                if source['id'] == dest_1['id']:
                    dest_1['value'] = source['value']

    for source in data2['values']:
        if source['id'] == dest['id']:
            dest['value'] = source['value']

with open("report.json", "w") as write_file:
    json.dump(data1, write_file, indent=2)

As I understood I need to check recursively whether file1.json has 'values' parameter and empty 'value' parameter inside that block. Moreover I couldn't touch source tests.json but only create another one file to save all changes.

DrShams
  • 1
  • 1
  • 1
    Does this answer your question? [Merge JSON data with Python](https://stackoverflow.com/questions/49812167/merge-json-data-with-python) – RoseGod Dec 20 '21 at 16:49
  • That comment can't help, different causes – DrShams Dec 21 '21 at 17:09
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Dec 28 '21 at 07:12

1 Answers1

0

That's because you're updating the root json object and getting a

{
    "tests": [...],
    "values": [...]
}

While what you want is to update individual "tests" from individual "values" and get just

{
    "tests": [...]
}

as a result.

Try looping through every object in both jsons.

Expurple
  • 877
  • 6
  • 13