简体   繁体   中英

changing the json using jq in bash

i have the following json

{
    "name" : "qwerty",
    "values" :[
    {
        "field1" : [
            "val1"
            ],
        "field2" : [
            "val2"
            ],
        "name1" : [["a", "b"], ["c", "d"]]
    },
    {
        "field1" : [
            "val3"
            ],
        "field2" : [
            "val4"
            ],
        "name1" : [["a", "b"], ["c", "d"]]
    },
    {
        "field1" : [
            "val5"
            ],
        "field2" : [
            "val6"
            ],
         "name1" : [["a", "b"], ["c", "d"]]
    }
    ]
}

I need to change the above json to the following using jq in bash

{
    "name" : "qwerty",
    "values" :[
    {
        "field1" :  "val1",
        "field2" :  "val2",
        "new_name" : [["a", "b"], ["c", "d"]]
    },
    {
        "field1" : "val3",
        "field2" : "val4",
        "new_name" : [["a", "b"], ["c", "d"]]
    },
    {
        "field1" : "val5",
        "field2" : "val6",
        "new_name" : [["a", "b"], ["c", "d"]]
    }
    ]

}

Here i am facing the following issues :

I tried parsing the inner json with tag values and replace the '[' ']' with spaces, however, when i try to put the "values" in a variable in the form of list, jq is prettifying and then showing each new line as a element of an array.

The number of inner jsons in the values array is not fixed.

Can some one please help me with framing the jq statement to be run in bash to make the required changes.

The snippet below should do what you want:

jq '{
    "name": .name, 
    "values": [ 
        {
            "field1" : .values[0].field1[0], 
            "field2" : .values[0].field2[0],
            "New_name": .values[0].name1  
        },
        {
            "field1" : .values[1].field1[0],
            "field2" : .values[1].field2[0],
            "new_name" : .values[1].name1
        },
        {
            "field1" : .values[2].field1[0],
            "field2" : .values[2].field2[0],
            "new_name" : .values[2].name1
        }
    ]  
}' < /tmp/input.json

EDIT

Since the number of objects are not fixed the snippet below will do:

jq '{ 
        "name" : .name,
        "values" : [
                .values[] as $in | 
                { 
                        "field1" : $in.field1[0],
                        "field2" : $in.field2[0],
                        "new_name" : $in.name1 
                }
        ]

}' < /tmp/input.json

This should work; I'm not sure if there is a way to refactor the assignments to field1 and field2 :

jq '.values[] |= (.field1=.field1[0] | .field2=.field2[0])' tmp1.json

Here is another solution.

  .values |= map({
     field1:   .field1[0],
     field2:   .field2[0],
     new_name: .name1
  })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM