简体   繁体   English

使用jq从文本输入中嵌套json

[英]nested json from text input using jq

I have text data which looks like (space separated snippet): First line is the header: 我有文本数据,看起来像(用空格分隔的片段):第一行是标题:

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

Using shell script or command line (preferably using jq) want to convert this to one nested json which would look like: 使用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
                    }
             }
        }

The appropriate approach to solving the problem using jq is essentially the same as would be used using awk : for each input line (except the first), parse it and emit the corresponding JSON object. 使用jq解决问题的合适方法与使用awk基本上相同:对于每条输入线(第一行除外),解析它并发出相应的JSON对象。 For the sake of clarity, let's define a function for handling each of the input lines (read in as a string): 为了清楚起见,让我们定义一个函数来处理每条输入线(以字符串形式读入):

def parse:
  [splits(" +")]
  | {(.[0]) : {type: .[1],
               input: (.[2] | fromjson),
               config: { file: .[3],
                           th: .[4] }} };

The solution is then simply: 那么解决方案就是:

{ dets: [inputs | parse] | add }

The appropriate invocation of jq would look like this: jq的适当调用如下所示:

jq -R -f program.jq input.txt

Note that omitting the -n command-line option here has the effect of skipping the header line. 请注意,此处省略-n命令行选项会跳过标题行。

This invocation will produce the JSON shown below. 该调用将产生如下所示的JSON。 Note that the text shown as JSON in the question is strictly speaking not JSON, but since the stated goal is to produce JSON, we'll leave it at that. 请注意,问题中显示为JSON的文本严格来说不是JSON,但是由于声明的目标是生成JSON,因此我们将其保留。

Caveats 注意事项

The above solution makes various assumptions, notably: 上述解决方案做出各种假设,尤其是:

1) the fields are "space-separated" in the sense of matching the regex " +"; 1)在匹配正则表达式“ +”的意义上,字段是“空格分隔的”; in particular, it is assumed that the array is presented without any embedded spaces. 特别地,假设呈现的阵列没有任何嵌入的空间。 2) the "input" field can be parsed using fromjson ; 2)可以使用fromjson解析“输入”字段; if there is any doubt about that, then use (fromjson? // .) instead. 如果对此有任何疑问,请改用(fromjson? // .)

Output 输出量

{
  "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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM