Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jq read .txt file and write the values to json file

Tags:

json

bash

jq

I want to use jq to parse a .txt file with a list of country codes and write them to the value in a JSON object.

Here is what I have so far:

cat myfile.json | 
jq -R -f test_id.txt 'select(.country == []).country = "test_id.txt"' > newfile.json

Where .txt file looks like this:

"NSC"
"KZC"
"KCC"
"KZL"
"NZG"
"VRU"
"ESM"
"KZF"
"SFU"
"EWF"
"KQY"
"KQV"

and my JSON looks like this:

{
  "scsRequestId": null,
  "includeMetadata": true,
  "includeHoldings": true,
  "country": [],
  "region": [],
  "oclcSymbol": []
}

Here is the error I am getting:

jq: error: syntax error, unexpected QQSTRING_START, expecting $end (Unix shell quoting issues?) at <top-level>, line 2:
"KZC"
jq: 1 compile error

I want the list of country codes to go into the country array.

like image 814
mjo Avatar asked Mar 29 '16 00:03

mjo


People also ask

Can jq write JSON?

jq is an amazing little command line utility for working with JSON data. We've written before about how you can use jq to parse JSON on the command line, but in this post I want to talk about using jq to create JSON data from scratch or make changes to existing data.

Does jq use JSONPath?

jp is a JSON processor for the command line using JSONPath (aka "a simpler jq, and with JSONPath").

What is jq JSON parser?

jq is a command line tool for parsing and modifying JSON. It is useful for extracting relevant bits of information from tools that output JSON, or REST APIs that return JSON. Mac users can install jq using homebrew ( brew install jq ); see here for more install options.

What is the output of jq?

jq usually outputs non-ASCII Unicode codepoints as UTF-8, even if the input specified them as escape sequences (like "\u03bc"). Using this option, you can force jq to produce pure ASCII output with every non-ASCII character replaced with the equivalent escape sequence.


1 Answers

-f's argument is the file to read the filter to run from. If you want to read data from a file, that's a use for --slurpfile, not -f.

Thus:

jq --slurpfile countries test_id.txt '.country=$countries' <myfile.json >newfile.json

When run with your provided inputs, the resulting contents in newfile.json are:

{
  "scsRequestId": null,
  "includeMetadata": true,
  "includeHoldings": true,
  "country": [
    "NSC",
    "KZC",
    "KCC",
    "KZL",
    "NZG",
    "VRU",
    "ESM",
    "KZF",
    "SFU",
    "EWF",
    "KQY",
    "KQV"
  ],
  "region": [],
  "oclcSymbol": []
}
like image 166
Charles Duffy Avatar answered Oct 22 '22 21:10

Charles Duffy