0

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?

1 Answer 1

1

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.

  1. sed-only
    < qjson.txt sed -e '1d;$d; s/^ //'
  1. hjson, jq, and sed
   < qjson.txt hjson -j | jq -r '.[] | (.,",")' | sed '$d'
  1. hjson and jq
    < qjson.txt hjson -j | jq -r '
        foreach .[] as $x (-1; .+1; 
          if . == 0 then $x else ",", $x end)'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.