Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

jq to merge two files with different formats

I have two files

file 1

{
    "version": "2",
    "run_id": "1",
    "crn": "crn",
    "date": "2022-11-02T02:09:30.629Z",
    "evidences": [
        {
            "version": "2",
            "id": "2d"
        },
        {
            "version": "2",
            "id": "3e"
        }  
]

file 2

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
        {
            "version": "3",
            "id": "3d"
        },
        {
            "version": "2",
            "id": "3f"
        }    
]

can we use jq to merge contents from file 2 into evidences section of file 1? which the result should like below

file 1

{
    "version": "2",
    "run_id": "1",
    "crn": "crn",
    "date": "2022-11-02T02:09:30.629Z",
    "evidences": [
        {
            "version": "2",
            "id": "2d"
        },
        {
            "version": "2",
            "id": "3e"
        },
        {
            "version": "3",
            "id": "3d"
        },
        {
            "version": "2",
            "id": "3f"
        }    
]

>Solution :

All you need is += to add up arrays while retaining the context, and input to access the second file:

jq '.evidences += input' file1.json file2.json
{
  "version": "2",
  "run_id": "1",
  "crn": "crn",
  "date": "2022-11-02T02:09:30.629Z",
  "evidences": [
    {
      "version": "2",
      "id": "2d"
    },
    {
      "version": "2",
      "id": "3e"
    },
    {
      "version": "3",
      "id": "3d"
    },
    {
      "version": "2",
      "id": "3f"
    }
  ]
}

Demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading