[英]nested json from text input using jq
我有文本数据,看起来像(用空格分隔的片段):第一行是标题:
name type input file th repeatNumber
S1 class [12,7,6,19] sfile1 -10 2
S2 class [12,7,6,19] sfile2 -5 1
S3 bottom [12,7,16] sfile3 -15 1
使用shell脚本或命令行(最好使用jq)想要将其转换为一个嵌套的json,如下所示:
'dets':{
'S1':{
'type':'class',
'input': [12,7,6,19],
'config':{
'file':'sfile1',
'th': -10
}
},
'S2':{
'type':'class',
'input': [12,7,6,19],
'config':{
'file':'sfile2',
'th': -5
}
},
'S3':{
'type':'bottom',
'input': [12,7,16],
'config':{
'file':'sfile3',
'th': -15
}
}
}
使用jq解决问题的合适方法与使用awk
基本上相同:对于每条输入线(第一行除外),解析它并发出相应的JSON对象。 为了清楚起见,让我们定义一个函数来处理每条输入线(以字符串形式读入):
def parse:
[splits(" +")]
| {(.[0]) : {type: .[1],
input: (.[2] | fromjson),
config: { file: .[3],
th: .[4] }} };
那么解决方案就是:
{ dets: [inputs | parse] | add }
jq的适当调用如下所示:
jq -R -f program.jq input.txt
请注意,此处省略-n
命令行选项会跳过标题行。
该调用将产生如下所示的JSON。 请注意,问题中显示为JSON的文本严格来说不是JSON,但是由于声明的目标是生成JSON,因此我们将其保留。
上述解决方案做出各种假设,尤其是:
1)在匹配正则表达式“ +”的意义上,字段是“空格分隔的”; 特别地,假设呈现的阵列没有任何嵌入的空间。 2)可以使用fromjson
解析“输入”字段; 如果对此有任何疑问,请改用(fromjson? // .)
。
{
"dets": {
"S1": {
"type": "class",
"input": [
12,
7,
6,
19
],
"config": {
"file": "sfile1",
"th": "-10"
}
},
"S2": {
"type": "class",
"input": [
12,
7,
6,
19
],
"config": {
"file": "sfile2",
"th": "-5"
}
},
"S3": {
"type": "bottom",
"input": [
12,
7,
16
],
"config": {
"file": "sfile3",
"th": "-15"
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.