简体   繁体   English

用JQ读取Json数组

[英]Read Json array with JQ

On my project I generate a json file here is an example (only one index): 在我的项目中我生成一个json文件这里是一个例子(只有一个索引):

[
    {
        "aaa": "lklklk",
        "bbb": "uiop",
        "kkk": "zeez",
        "lll": 3,
        "_source": {
          "element1": "zzzz",
          "element2": "eeee",
          "element3": "hhhhh",
          "element4": "jjjjjj",
          "@timestamp": "2019-07-31T08:32:45.000Z",
          "element5": "1",
          "element6": "6768",
          "element7": "gggg",
          "element8": "ppppp"
        },
        {
        "aaa": "lklklk",
        "bbb": "uiop",
        "kkk": "zeez",
        "lll": 3,
        "_source": {
          "element1": "zzzz",
          "element2": "eeee",
          "element3": "hhhhh",
          "element4": "jjjjjj",
          "@timestamp": "2019-07-31T08:32:45.000Z",
          "element5": "1",
          "element6": "6768",
          "element7": "gggg",
          "element8": "ppppp"
        },
]

I use this command 我用这个命令

var1=$(jq '.['$cpt'] | ._source .element1' file.json) 

and I can receive my value, however I receive this error: 我可以收到我的价值,但是收到这个错误:

jq error Cannot index number with number jq错误无法使用数字索引编号

I already try with 我已经尝试过了

var1=$(jq '.[] | ._source .element1' file.json) 

but I receive all data and I need to receive the data by index. 但我收到所有数据,我需要通过索引接收数据。

Here is my bash code: 这是我的bash代码:

while (($verification!=1))
  do
          elementa[$cpt]=$(jq '.['$cpt'] | ._source .element1' $File.json)
          elementb[$cpt]=$(jq '.['$cpt'] | ._source .element2' $File.json)
          elementc[$cpt]=$(jq '.['$cpt'] | ._source .element3' $File.json)
          elementd[$cpt]=$(jq '.['$cpt'] | ._source .element4' $File.json)
          elemente[$cpt]=$(jq '.['$cpt'] | ._source .element5' $File.json)
          elementf[$cpt]=$(jq '.['$cpt'] | ._source .element6' $File.json)
          elementg[$cpt]=$(jq '.['$cpt'] | ._source .element7' $File.json)
          elementh[$cpt]=$(jq '.['$cpt'] | ._source ."@timestamp"' $File.json)
          elementi[$cpt]=$(jq '.['$cpt'] | ._source .element8' $File.json)
done

for each line I receive the same error :/ Do you know why I have this error? 对于每一行,我收到同样的错误:/你知道为什么我有这个错误吗?

Thank you in advance. 先感谢您。

[EDIT: This A has been updated to reflect an update to the Q.] [编辑:此A已更新,以反映Q的更新。]

  1. The given input is (still) not valid JSON, but after fixing it in accordance with the description of the problem, you could you use this jq filter to extract .element1: 给定的输入是(仍然)无效的JSON,但在根据问题的描述修复它之后,您可以使用此jq过滤器来提取.element1:

     .[0]._source.element1 

    To extract all the items in the first ._source, you could simply write: 要提取第一个._source中的所有项目,您只需编写:

     .[0]._source[] 

    Or if you want to absolutely sure about the ordering: 或者,如果您想完全确定订购:

     .[0]._source[ "element1","element2","element3","element4","@timestamp","element5","element6","element7","element8"] 

    Either way, extracting all the items at once (at least for each group) is probably the way to go, even if it really is necessary to process each separately outside jq. 无论哪种方式,一次提取所有项目(至少对于每个组)可能是要走的路,即使真的有必要在jq之外单独处理每个项目。

  2. The line: 这条线:

     var1=$(jq '.['$cpt'] | ._source .element1' file.json) 

    is a jumble (as are all the other lines like it). 是一个混乱(像所有其他线路一样)。 Evidently you meant something like this: 显然你的意思是这样的:

     var1=$(jq ".[\\"$cpt\\"] | ._source .element1" file.json) 

    as $cpt is elsewhere evidently a bash variable. 因为$cpt在其他地方显然是一个bash变量。 Even if that is what you intended, though, it would be better to pass in the bash variable in some other way, eg if $cpt is an integer: 即使这是你想要的,最好以其他方式传递bash变量,例如,如果$ cpt是一个整数:

     var1=$(jq --argjson cpt "$cpt" '.[$cpt] | ._source .element1' file.json) 

    Of course this still does not address the mismatch with the sample JSON. 当然,这仍然没有解决与样本JSON的不匹配问题。

  3. There are many SO Q&As about populating bash variables with values from a single run of jq. 关于使用来自单个jq运行的值填充bash变量有很多SO Q&As。 See eg jq - return json as bash array 参见例如jq - 将json作为bash数组返回

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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