1

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.

4
  • Is there really an array in the JSON file? It needs to be like [ { ... }, { ... }, ...] Commented Jul 31, 2019 at 19:13
  • Show how the file looks with multiple elements. Commented Jul 31, 2019 at 19:13
  • Hello yes sorry, here is multiple element. I updated the post. Commented Jul 31, 2019 at 20:03
  • The sample input is still invalid JSON. You could use jq to diagnose the problems [plural!], but feel free to use an online service such as jsonlint.com :-) Commented Aug 1, 2019 at 12:42

1 Answer 1

3

[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, e.g. 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 e.g. jq - return json as bash array

Sign up to request clarification or add additional context in comments.

8 Comments

Hi De pointe, thank you for this detail :) I would like to have one element in one var. Because I need to verify different elements after that. SO how can I put one element in one var ? I need to extract data by data and by index.
I receive always the same error: jq: error (at file.json:1): Cannot index number with number jq: error (at file.json:2): Cannot index number with number
If $cpt is an integer, the simplest would be to use --argjson instead of --arg.
Hi Peak, yes and thank you for that. However the errors are always present :/ and I don't understand the problem ><
Have you fixed the JSON? Try running jq empty file.json
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.