简体   繁体   中英

Read Json array with JQ

On my project I generate a json file here is an example (only one index):

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

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:

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.]

  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:

     .[0]._source.element1 

    To extract all the items in the first ._source, you could simply write:

     .[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.

  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. 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:

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

    Of course this still does not address the mismatch with the sample JSON.

  3. There are many SO Q&As about populating bash variables with values from a single run of jq. See eg jq - return json as bash array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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