lundi 10 août 2020

Jq - add a key/value to one JSON file if such key/value exists in another JSON file

There is one JSON file - landline.json

[
    {
        "City": "San Francisco",
        "Person": "John Doe",
        "Landline": false
    },
    {
        "City": "Los Angeles",
        "Person": "Steve Smith",
        "Landline": false
    }
]

And there is another JSON file - mobile.json

[
    {
        "City": "San Francisco",
        "Person": "John Doe",
        "Mobile": false
    },
    {
        "City": "Los Angeles",
        "Person": "Jenny Miller",
        "Mobile": false
    }
]

Question is how using jq's filters and condition statement create a new file/array adding after the "Landline": false "Mobile": false for John Doe? So the resulting array should look like the following:

[
    {
        "City": "San Francisco",
        "Person": "John Doe",
        "Landline": false,
        "Mobile": false
    },
    {
        "City": "Los Angeles",
        "Person": "Steve Smith",
        "Landline": false
    }
]

So the algorithm should be following - if in the mobile.json there is "Person": "John Doe" entry and in the same block there is "Mobile": false entry, then add "Mobile": false entry after "Landline": false for "Person": "John Doe" in landline.json.

Aucun commentaire:

Enregistrer un commentaire