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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With