[英]How to create a specific result using JQ (jq parse json)
I have massive like this:我有大量这样的:
2254003131908096 39480500160 39763833120
2296334329577472 36713833920 37747166400
2297708719112192 39830499360 41430500640
Which JQ command should I use to get the result like this:我应该使用哪个 JQ 命令来获得这样的结果:
"2254003131908096": {
{
"exchange": "39480500160",
"replication": "39763833120"
}
}
"2296334329577472": {
{
"exchange": "36713833920",
"replication": "39763833120"
}
}
"2297708719112192": {
{
"exchange": "39830499360",
"replication": "41430500640"
}
}
Help me please.请帮帮我。
Your sample output is invalid JSON. I assume you want to have one object with one field per input row.您的示例 output 无效 JSON。我假设您想要一个 object,每个输入行有一个字段。
Read in raw text using -R
, then split by space using /
, and reduce all the input to one object by setting the according fields.使用
-R
读取原始文本,然后使用/
按空格拆分,并通过设置相应的字段将所有输入减少到一个 object。
jq -Rn '
[inputs / " "] | reduce .[] as $line ({};
.[$line[0]] = {exchange: $line[1], replication: $line[2]}
)
'
{
"2254003131908096": {
"exchange": "39480500160",
"replication": "39763833120"
},
"2296334329577472": {
"exchange": "36713833920",
"replication": "37747166400"
},
"2297708719112192": {
"exchange": "39830499360",
"replication": "41430500640"
}
}
You can use reduce
.你可以使用
reduce
。
Filter筛选
reduce (inputs / " " ) as [$k,$e,$r] ({}; .+{($k):{exchange:$e,replication:$r}})
Input输入
2254003131908096 39480500160 39763833120
2296334329577472 36713833920 37747166400
2297708719112192 39830499360 41430500640
Output Output
{
"2254003131908096": {
"exchange": "39480500160",
"replication": "39763833120"
},
"2296334329577472": {
"exchange": "36713833920",
"replication": "37747166400"
},
"2297708719112192": {
"exchange": "39830499360",
"replication": "41430500640"
}
}
Demo演示
https://jqplay.org/s/9uugFhWQtt https://jqplay.org/s/9uugFhWQtt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.