vamsi
vamsi

Reputation: 323

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.

Upvotes: 3

Views: 429

Answers (3)

jq170727
jq170727

Reputation: 14655

Here is another solution.

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

Upvotes: 0

chepner
chepner

Reputation: 531165

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

Upvotes: 2

Tiago Lopo
Tiago Lopo

Reputation: 7959

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

Upvotes: 2

Related Questions