0

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"
            }
        ]
    }
}
CoolZero
  • 109
  • 7
  • There's a discrepancy between the object you're trying to insert in your attempt and the one inserted in the wanted result. – Benjamin W. Sep 14 '21 at 07:58

1 Answers1

0

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

lojza
  • 1,823
  • 2
  • 13
  • 23