Home > Back-end >  Append values to beginning and end of every other line in ndjson file with Python or jq
Append values to beginning and end of every other line in ndjson file with Python or jq

Time:11-16

I have a ndjson file with first several lines as such:

{"index":{}}
{"data":"John Doe","edType":"FG"}
{"index":{}}
{"data":"Jane Doe","edType":"GG"}
{"index":{}}
{"data":"Jim Doe","edType":"FG"}

Using Python (I'm on 3.10) I need to append additional data to beginning and end of each line that it not an index line. The result would look like this:

{"index":{}}
{"name":{"data":"John Doe","edType":"FG"}}
{"index":{}}
{"name":{"data":"Jane Doe","edType":"GG"}}
{"index":{}}
{"name":{"data":"Jim Doe","edType":"FG"}}

I've tried this using several suggestions here which seem to only apply to text files. I am wondering if I have to do this with the JSON module or not.

Alternatively, if easier to attain in jq, I'm happy to use that too.

CodePudding user response:

This is trivially done with jq:

jq -c '., {"name": input}'
  • -c specifies compact output, so we have one output line per JSON value (matching the input format)
  • . refers to the current input item
  • input retrieves the next input item (consuming it, so it's no longer eligible to become .)

...and the sequence repeats as long as more inputs are available.

  •  Tags:  
  • json
  • Related