jq读取.txt文件并将值写入json文件 [英] jq read .txt file and write the values to json file
本文介绍了jq读取.txt文件并将值写入json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用jq解析具有国家/地区代码列表的.txt文件,并将其写入json对象中的值.
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
.txt文件如下所示
Where .txt file looks like this
"NSC"
"KZC"
"KCC"
"KZL"
"NZG"
"VRU"
"ESM"
"KZF"
"SFU"
"EWF"
"KQY"
"KQV"
我的json看起来像这样
and my json looks like this
{
"scsRequestId": null,
"includeMetadata": true,
"includeHoldings": true,
"country": [],
"region": [],
"oclcSymbol": []
}
这是我遇到的错误
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.
感谢您的帮助
推荐答案
-f
的参数是读取要运行的过滤器的文件.如果要从文件中读取数据,这是--slurpfile
而不是-f
的用途.
-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
.
因此:
jq --slurpfile countries test_id.txt '.country=$countries' <myfile.json >newfile.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": []
}
这篇关于jq读取.txt文件并将值写入json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文