Home > front end >  JQ: Add object to nested json with the same key names
JQ: Add object to nested json with the same key names

Time:09-17

I having trouble of getting my json append with a new object into the config -> list -> key(vehicles) -> Rows. But then only for vehicles.

Im trying it with JQ: cat file.json | jq '.config.list[].rows[] = {"data":[{"key":"fort","value":"K"},{"key":"seat","value":"leon"}],"default":false}' But with this it is replacing and not appending because of the same names ?

Object that needs to

                    {
                        "data": [
                            {
                                "key": "bike",
                                "value": "yyy"
                            },
                            {
                                "key": "car",
                                "value": "xxx"
                            }
                        ],
                        "default": false
                    }

Source Json:

{
    "id": "1234",
    "name": "CatList",
    "config": {
        "list": [
            {
                "key": "vehicles",
                "rows": [

                    {
                        "data": [
                            {
                                "key": "bike",
                                "value": "yyy"
                            },
                            {
                                "key": "car",
                                "value": "xxx"
                            }
                        ],
                        "default": false
                    }


                ]
            },
            {
                "key": "boots",
                "rows": []
            }
        ],
        "data": [
            {
                "key": "GROUPS",
                "value": "false"
            }
        ]
    }
}

Wanted result:

{
    "id": "1234",
    "name": "CatList",
    "config": {
        "list": [
            {
                "key": "vehicles",
                "rows": [

                    {
                        "data": [
                            {
                                "key": "bike",
                                "value": "yyy"
                            },
                            {
                                "key": "car",
                                "value": "xxx"
                            }
                        ],
                        "default": false
                    },
                    {                             
                        "data": [                  <-----
                            {                      <-----
                                "key": "bike",     <-----
                                "value": "yyy"     <-----
                            },                     <-----
                            {                      <-----
                                "key": "car",      <-----
                                "value": "xxx"     <-----
                            }                      <-----
                        ],                         <-----
                        "default": false           <-----
                    }


                ]
            },
            {
                "key": "boots",
                "rows": []
            }
        ],
        "data": [
            {
                "key": "GROUPS",
                "value": "false"
            }
        ]
    }
}

CodePudding user response:

You were close

jq '.config.list[.config.list|map(.key=="vehicles")|index(true)].rows  = [{"data":[{"key":"fot","value":"K"},{"key":"seat","value":"leon"}],"default":false}]'

see https://stackoverflow.com/a/42248841/2235381

  • Related