Home > Mobile >  jq add character before and after every row JSON
jq add character before and after every row JSON

Time:09-23

I want print "[]" in every row with jq to make a big file json proof

I have:

{"test":"index","test2":"fdsdfs","test3":"dfs0D0WOQAA3"}
{"test":"index","test2":"fdsdfs","test3":"dfs0D0WOQAA3"}
{"test":"index","test2":"fdsdfs","test3":"dfs0D0WOQAA3"}
{"test":"index","test2":"fdsdfs","test3":"dfs0D0WOQAA3"}

What i want:

[[{"test":"index","test2":"fdsdfs","test3":"dfs0D0WOQAA3"}]
[{"test":"index","test2":"fdsdfs","test3":"dfs0D0WOQAA3"}]
[{"test":"index","test2":"fdsdfs","test3":"dfs0D0WOQAA3"}]
[{"test":"index","test2":"fdsdfs","test3":"dfs0D0WOQAA3"}]]

Now i can push the json line by line to a program.

CodePudding user response:

If you input are just plain objects:
Use --slurp to create a single array, then use map([ . ]) to wrap each object in his own array

jq --slurp --compact-output 'map([ . ])'
[[{"test":"index","test2":"fdsdfs","test3":"dfs0D0WOQAA3"}],[{"test":"index","test2":"fdsdfs","test3":"dfs0D0WOQAA3"}], ...

JqPlay demo

CodePudding user response:

Assuming your file contains a stream of stand-alone JSON objects, the filter is simply [.]. The filter will be applied to each input object.

jq -c '[.]' file.json

Output:

[{"test":"index","test2":"fdsdfs","test3":"dfs0D0WOQAA3"}]
[{"test":"index","test2":"fdsdfs","test3":"dfs0D0WOQAA3"}]
[{"test":"index","test2":"fdsdfs","test3":"dfs0D0WOQAA3"}]
[{"test":"index","test2":"fdsdfs","test3":"dfs0D0WOQAA3"}]

To wrap everything in one big array:

jq -s . file.json
# or:
jq -s <file.json

Output:

[
  {
    "test": "index",
    "test2": "fdsdfs",
    "test3": "dfs0D0WOQAA3"
  },
  {
    "test": "index",
    "test2": "fdsdfs",
    "test3": "dfs0D0WOQAA3"
  },
  {
    "test": "index",
    "test2": "fdsdfs",
    "test3": "dfs0D0WOQAA3"
  },
  {
    "test": "index",
    "test2": "fdsdfs",
    "test3": "dfs0D0WOQAA3"
  }
]

And to wrap each single item in an array and all arrays in a top-level array:

jq 'map([.])' file.json

Output:

[
  [
    {
      "test": "index",
      "test2": "fdsdfs",
      "test3": "dfs0D0WOQAA3"
    }
  ],
  [
    {
      "test": "index",
      "test2": "fdsdfs",
      "test3": "dfs0D0WOQAA3"
    }
  ],
  [
    {
      "test": "index",
      "test2": "fdsdfs",
      "test3": "dfs0D0WOQAA3"
    }
  ],
  [
    {
      "test": "index",
      "test2": "fdsdfs",
      "test3": "dfs0D0WOQAA3"
    }
  ]
]

CodePudding user response:

I assume you just forgot to put the commas in your otherwise valid JSON output, in which case you can find reasonable answers elsewhere on this page.

If not, and your desired output is accurate as shown, one way to solve it with jq is to read in the lines as raw text, prepend and append to certain lines as you see fit, and output again as raw text:

jq -Rnr '[inputs] | (first, .[]) |= "["   . | (last, .[])  = "]" | .[]'
[[{"test":"index","test2":"fdsdfs","test3":"dfs0D0WOQAA3"}]
[{"test":"index","test2":"fdsdfs","test3":"dfs0D0WOQAA3"}]
[{"test":"index","test2":"fdsdfs","test3":"dfs0D0WOQAA3"}]
[{"test":"index","test2":"fdsdfs","test3":"dfs0D0WOQAA3"}]]

Demo

But, to be fair, you actually don't need a JSON processor if it is just plain text being manipulated. Using sed or awk would be more reasonable choices here.

  • Related