Reputation: 926
Let's say I have the following JSON
[
{
name : "A",
value : "1"
},
{
name : "B",
value : "5"
},
{
name : "E",
value : "8"
}
]
and I simply want to to be like
{
name : "A",
value : "1"
},
{
name : "B",
value : "5"
},
{
name : "E",
value : "8"
}
I used jq normal filter so jq'.[]', however I get a list of objects separated by a return as such:
{
name : "A",
value : "1"
}
{
name : "B",
value : "5"
}
{
name : "E",
value : "8"
}
Notice that the commas between the objects have magically vanished. Using reduce would work only if the object is indexed by the name let's say, I used the following:
jq 'reduce .[] as $i ({}; .[$i.name] = $i)'
Anybody did run into a similar situation?
Upvotes: 0
Views: 649
Reputation: 116690
Neither the input as shown nor the desired output is valid as JSON or as a JSON stream, so the question seems questionable, and the following responses are accordingly offered with the caveat that they probably should be avoided.
It should also be noted that, except for the sed-only approach, the solutions offered here produce comma-separated-JSON, which may not be what is desired.
They assume that the quasi-JSON input is in a file qjson.txt
.
< qjson.txt sed -e '1d;$d; s/^ //'
< qjson.txt hjson -j | jq -r '.[] | (.,",")' | sed '$d'
< qjson.txt hjson -j | jq -r '
foreach .[] as $x (-1; .+1;
if . == 0 then $x else ",", $x end)'
Upvotes: 1