-1

I would like to merge to json objects using jq. The files differ in the Label and Description arrays. In the result all descriptions in all languages should be present. The object might also be more complex so the solution should work recursively.

file 1:

{
    "A": {
        "Description": [{
                "Language": "",
                "Text": "Default Text 1"
            }, {
                "Language": "de",
                "Text": "German Text 1"
            }
        ],
        "Label": [{
                "Language": "",
                "Text": "Default Text 2"
            }, {
                "Language": "de",
                "Text": "German Text 2"
            }
        ]
    }
}

file 2:

{
    "A": {
        "Description": [{
                "Language": "en",
                "Text": "English Text 1"
            }
        ],
        "Label": [ {
                "Language": "en",
                "Text": "English Text 2"
            }
        ]

    }

}

Expected Result:

{
    "A": {
        "Description": [{
                "Language": "",
                "Text": "Default Text 1"
            }, {
                "Language": "de",
                "Text": "German Text 1"
            }, {
                "Language": "en",
                "Text": "English Text 1"
            }

        ],
        "Label": [{
                "Language": "",
                "Text": "Default Text 2"
            }, {
                "Language": "de",
                "Text": "German Text 2"
            }, {
                "Language": "en",
                "Text": "English Text 2"
            }
        ]
    }
}

Thanks, Alex

Alex
  • 19
  • 3

2 Answers2

0

Well, I think this question is duplicated unless I am missing something: How to merge 2 json file using jq?

0

This will give you the expected result with the sample provided. Perhaps it will get you started towards finding a more general solution.

jq 'reduce inputs as $i (.; 
                            .A.Description += $i.A.Description
                          | .A.Label += $i.A.Label
                        )' File1.json File2.json

I think you would have more luck if you searched for "merge arrays." I found several cases that looked similar to yours.

user197693
  • 1,935
  • 10
  • 8