Error message “Cannot index array with string 'Title'” when parsing JSON data with jq2019 Community Moderator ElectionParsing JSON with JQLinux Shell - How to remove escape characters generated by JQ when json is read with sed?

Generic TVP tradeoffs?

Why is indicated airspeed rather than ground speed used during the takeoff roll?

Have the tides ever turned twice on any open problem?

What can I do if I am asked to learn different programming languages very frequently?

How to get the n-th line after a grepped one?

What is the significance behind "40 days" that often appears in the Bible?

How do hiring committees for research positions view getting "scooped"?

Should I use acronyms in dialogues before telling the readers what it stands for in fiction?

Do native speakers use "ultima" and "proxima" frequently in spoken English?

What is the plural TO OF sth

Print a physical multiplication table

Why Choose Less Effective Armour Types?

Can a wizard cast a spell during their first turn of combat if they initiated combat by releasing a readied spell?

Constant Current LED Circuit

How difficult is it to simply disable/disengage the MCAS on Boeing 737 Max 8 & 9 Aircraft?

Probably overheated black color SMD pads

Is this an example of a Neapolitan chord?

Can other pieces capture a threatening piece and prevent a checkmate?

What does "Four-F." mean?

Calculate the frequency of characters in a string

In what cases must I use 了 and in what cases not?

TikZ-decoration: control decoration amplitude along curve

Why is a polar cone a closed set?

Error: "inconsistent hash". Workers crash and node is unable to connect to others



Error message “Cannot index array with string 'Title'” when parsing JSON data with jq



2019 Community Moderator ElectionParsing JSON with JQLinux Shell - How to remove escape characters generated by JQ when json is read with sed?










6
















"content": [

"Title": "abc",
"brand": "xyz",
"size": "5 g",
"date": "2019-01-01",
"details":
"Temperature": [

"value": "90",
"characteristics":"Normal"
,

"value":"100",
"characteristics":"high"
,


"value":"80",
"characteristics":"low"

],

"certifications": [

"value": "based",
"characteristics":"pass"
,


"value": "50",
"characteristics":"failed"

]
,

"formats":
"city": "NYC",
"id": "007",
"manufacture":""
,
"innerDetails": [

"contains": "abc",
"panel":"xyz",
"values":[

"name":"abc",
"value":"10"
,

"name":"xyz",
"value":"20"

]

]

]



I have tried the below approach, but getting the error




Cannot index array with string "Title"




jq -r '.content[]|[.Title,.brand,.characteristics,.value]' $jsonfile.


I was trying on the same line with other sections, but getting the same error.



How do I solve this issue?



Expected output:



abc,xyz,90,Normal.
abc,xyz,100,high.
abc,xyz,80,low









share|improve this question




























    6
















    "content": [

    "Title": "abc",
    "brand": "xyz",
    "size": "5 g",
    "date": "2019-01-01",
    "details":
    "Temperature": [

    "value": "90",
    "characteristics":"Normal"
    ,

    "value":"100",
    "characteristics":"high"
    ,


    "value":"80",
    "characteristics":"low"

    ],

    "certifications": [

    "value": "based",
    "characteristics":"pass"
    ,


    "value": "50",
    "characteristics":"failed"

    ]
    ,

    "formats":
    "city": "NYC",
    "id": "007",
    "manufacture":""
    ,
    "innerDetails": [

    "contains": "abc",
    "panel":"xyz",
    "values":[

    "name":"abc",
    "value":"10"
    ,

    "name":"xyz",
    "value":"20"

    ]

    ]

    ]



    I have tried the below approach, but getting the error




    Cannot index array with string "Title"




    jq -r '.content[]|[.Title,.brand,.characteristics,.value]' $jsonfile.


    I was trying on the same line with other sections, but getting the same error.



    How do I solve this issue?



    Expected output:



    abc,xyz,90,Normal.
    abc,xyz,100,high.
    abc,xyz,80,low









    share|improve this question


























      6












      6








      6









      "content": [

      "Title": "abc",
      "brand": "xyz",
      "size": "5 g",
      "date": "2019-01-01",
      "details":
      "Temperature": [

      "value": "90",
      "characteristics":"Normal"
      ,

      "value":"100",
      "characteristics":"high"
      ,


      "value":"80",
      "characteristics":"low"

      ],

      "certifications": [

      "value": "based",
      "characteristics":"pass"
      ,


      "value": "50",
      "characteristics":"failed"

      ]
      ,

      "formats":
      "city": "NYC",
      "id": "007",
      "manufacture":""
      ,
      "innerDetails": [

      "contains": "abc",
      "panel":"xyz",
      "values":[

      "name":"abc",
      "value":"10"
      ,

      "name":"xyz",
      "value":"20"

      ]

      ]

      ]



      I have tried the below approach, but getting the error




      Cannot index array with string "Title"




      jq -r '.content[]|[.Title,.brand,.characteristics,.value]' $jsonfile.


      I was trying on the same line with other sections, but getting the same error.



      How do I solve this issue?



      Expected output:



      abc,xyz,90,Normal.
      abc,xyz,100,high.
      abc,xyz,80,low









      share|improve this question

















      "content": [

      "Title": "abc",
      "brand": "xyz",
      "size": "5 g",
      "date": "2019-01-01",
      "details":
      "Temperature": [

      "value": "90",
      "characteristics":"Normal"
      ,

      "value":"100",
      "characteristics":"high"
      ,


      "value":"80",
      "characteristics":"low"

      ],

      "certifications": [

      "value": "based",
      "characteristics":"pass"
      ,


      "value": "50",
      "characteristics":"failed"

      ]
      ,

      "formats":
      "city": "NYC",
      "id": "007",
      "manufacture":""
      ,
      "innerDetails": [

      "contains": "abc",
      "panel":"xyz",
      "values":[

      "name":"abc",
      "value":"10"
      ,

      "name":"xyz",
      "value":"20"

      ]

      ]

      ]



      I have tried the below approach, but getting the error




      Cannot index array with string "Title"




      jq -r '.content[]|[.Title,.brand,.characteristics,.value]' $jsonfile.


      I was trying on the same line with other sections, but getting the same error.



      How do I solve this issue?



      Expected output:



      abc,xyz,90,Normal.
      abc,xyz,100,high.
      abc,xyz,80,low






      jq






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 55 mins ago









      Andy Lester

      425416




      425416










      asked 9 hours ago









      samsam

      386




      386




















          1 Answer
          1






          active

          oldest

          votes


















          8














          You are not getting Cannot index array with string "Title" with that command, you are getting



          [
          "abc",
          "xyz",
          null,
          null
          ]


          since there is no characteristics or value key in the objects of the contents array (they are keys in the .details.Temperature sub-array).



          The command that would have given you that message is:



          jq -r '.[] | [.Title,.brand,.characteristics,.value]' "$jsonfile"


          or



          jq -r '.content | [.Title,.brand,.characteristics,.value]' "$jsonfile"


          Missing out the content key lookup, or failing to get the elements of the content array, yields an array of one object rather than the object itself. And you can't index an array with a string.




          Assuming you want CSV output:



          $ jq -r '.content[] | .details.Temperature[] as $t | [.Title,.brand,$t.value,$t.characteristics] | @csv' file.json
          "abc","xyz","90","Normal"
          "abc","xyz","100","high"
          "abc","xyz","80","low"


          The <object(s)> as <variable> acts like a loop in jq, so what happens here is that $t will be assigned each element of .details.Temperature[] in turn, and for each element, a new array is constructed. The array is passed through @csv which will output CSV-formatted rows.



          jq will always double quote the fields of its CSV output. To get rid of unnecessary quotes:



          jq -r '...as above...' file.json | csvformat


          (csvformat is part of csvkit)



          Or, you may want to use @tsv in place of @csv to get tab-delimited output instead.






          share|improve this answer




















          • 2





            Kusal Thank you for your inputs. I am using above thing with for loop. details below. for field in Temperature certifications ; do echo $field :: jq --arg field "$field" -r ' .content[] | .details."$field"[] as $t | [.Title,.brand,$t.value,$t.characteristics] | @csv' file.json done. but getting "jq: error: try .["field"] instead of .field for unusually named fields at <top-level>, line 1:"

            – sam
            8 hours ago











          • I got this, I was not able to correlate and was putting .details.$[field] or .details."$field". Now I have changed that to .details[$field][] and it's working fine now.

            – sam
            7 hours ago











          • @sam Sorry, I was elsewhere. Yes, .details[$field][] or .["details"][$field][] is the correct syntax.

            – Kusalananda
            7 hours ago











          • I have up-voted and thank you for your support.

            – sam
            1 hour ago










          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f506789%2ferror-message-cannot-index-array-with-string-title-when-parsing-json-data-wi%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          8














          You are not getting Cannot index array with string "Title" with that command, you are getting



          [
          "abc",
          "xyz",
          null,
          null
          ]


          since there is no characteristics or value key in the objects of the contents array (they are keys in the .details.Temperature sub-array).



          The command that would have given you that message is:



          jq -r '.[] | [.Title,.brand,.characteristics,.value]' "$jsonfile"


          or



          jq -r '.content | [.Title,.brand,.characteristics,.value]' "$jsonfile"


          Missing out the content key lookup, or failing to get the elements of the content array, yields an array of one object rather than the object itself. And you can't index an array with a string.




          Assuming you want CSV output:



          $ jq -r '.content[] | .details.Temperature[] as $t | [.Title,.brand,$t.value,$t.characteristics] | @csv' file.json
          "abc","xyz","90","Normal"
          "abc","xyz","100","high"
          "abc","xyz","80","low"


          The <object(s)> as <variable> acts like a loop in jq, so what happens here is that $t will be assigned each element of .details.Temperature[] in turn, and for each element, a new array is constructed. The array is passed through @csv which will output CSV-formatted rows.



          jq will always double quote the fields of its CSV output. To get rid of unnecessary quotes:



          jq -r '...as above...' file.json | csvformat


          (csvformat is part of csvkit)



          Or, you may want to use @tsv in place of @csv to get tab-delimited output instead.






          share|improve this answer




















          • 2





            Kusal Thank you for your inputs. I am using above thing with for loop. details below. for field in Temperature certifications ; do echo $field :: jq --arg field "$field" -r ' .content[] | .details."$field"[] as $t | [.Title,.brand,$t.value,$t.characteristics] | @csv' file.json done. but getting "jq: error: try .["field"] instead of .field for unusually named fields at <top-level>, line 1:"

            – sam
            8 hours ago











          • I got this, I was not able to correlate and was putting .details.$[field] or .details."$field". Now I have changed that to .details[$field][] and it's working fine now.

            – sam
            7 hours ago











          • @sam Sorry, I was elsewhere. Yes, .details[$field][] or .["details"][$field][] is the correct syntax.

            – Kusalananda
            7 hours ago











          • I have up-voted and thank you for your support.

            – sam
            1 hour ago















          8














          You are not getting Cannot index array with string "Title" with that command, you are getting



          [
          "abc",
          "xyz",
          null,
          null
          ]


          since there is no characteristics or value key in the objects of the contents array (they are keys in the .details.Temperature sub-array).



          The command that would have given you that message is:



          jq -r '.[] | [.Title,.brand,.characteristics,.value]' "$jsonfile"


          or



          jq -r '.content | [.Title,.brand,.characteristics,.value]' "$jsonfile"


          Missing out the content key lookup, or failing to get the elements of the content array, yields an array of one object rather than the object itself. And you can't index an array with a string.




          Assuming you want CSV output:



          $ jq -r '.content[] | .details.Temperature[] as $t | [.Title,.brand,$t.value,$t.characteristics] | @csv' file.json
          "abc","xyz","90","Normal"
          "abc","xyz","100","high"
          "abc","xyz","80","low"


          The <object(s)> as <variable> acts like a loop in jq, so what happens here is that $t will be assigned each element of .details.Temperature[] in turn, and for each element, a new array is constructed. The array is passed through @csv which will output CSV-formatted rows.



          jq will always double quote the fields of its CSV output. To get rid of unnecessary quotes:



          jq -r '...as above...' file.json | csvformat


          (csvformat is part of csvkit)



          Or, you may want to use @tsv in place of @csv to get tab-delimited output instead.






          share|improve this answer




















          • 2





            Kusal Thank you for your inputs. I am using above thing with for loop. details below. for field in Temperature certifications ; do echo $field :: jq --arg field "$field" -r ' .content[] | .details."$field"[] as $t | [.Title,.brand,$t.value,$t.characteristics] | @csv' file.json done. but getting "jq: error: try .["field"] instead of .field for unusually named fields at <top-level>, line 1:"

            – sam
            8 hours ago











          • I got this, I was not able to correlate and was putting .details.$[field] or .details."$field". Now I have changed that to .details[$field][] and it's working fine now.

            – sam
            7 hours ago











          • @sam Sorry, I was elsewhere. Yes, .details[$field][] or .["details"][$field][] is the correct syntax.

            – Kusalananda
            7 hours ago











          • I have up-voted and thank you for your support.

            – sam
            1 hour ago













          8












          8








          8







          You are not getting Cannot index array with string "Title" with that command, you are getting



          [
          "abc",
          "xyz",
          null,
          null
          ]


          since there is no characteristics or value key in the objects of the contents array (they are keys in the .details.Temperature sub-array).



          The command that would have given you that message is:



          jq -r '.[] | [.Title,.brand,.characteristics,.value]' "$jsonfile"


          or



          jq -r '.content | [.Title,.brand,.characteristics,.value]' "$jsonfile"


          Missing out the content key lookup, or failing to get the elements of the content array, yields an array of one object rather than the object itself. And you can't index an array with a string.




          Assuming you want CSV output:



          $ jq -r '.content[] | .details.Temperature[] as $t | [.Title,.brand,$t.value,$t.characteristics] | @csv' file.json
          "abc","xyz","90","Normal"
          "abc","xyz","100","high"
          "abc","xyz","80","low"


          The <object(s)> as <variable> acts like a loop in jq, so what happens here is that $t will be assigned each element of .details.Temperature[] in turn, and for each element, a new array is constructed. The array is passed through @csv which will output CSV-formatted rows.



          jq will always double quote the fields of its CSV output. To get rid of unnecessary quotes:



          jq -r '...as above...' file.json | csvformat


          (csvformat is part of csvkit)



          Or, you may want to use @tsv in place of @csv to get tab-delimited output instead.






          share|improve this answer















          You are not getting Cannot index array with string "Title" with that command, you are getting



          [
          "abc",
          "xyz",
          null,
          null
          ]


          since there is no characteristics or value key in the objects of the contents array (they are keys in the .details.Temperature sub-array).



          The command that would have given you that message is:



          jq -r '.[] | [.Title,.brand,.characteristics,.value]' "$jsonfile"


          or



          jq -r '.content | [.Title,.brand,.characteristics,.value]' "$jsonfile"


          Missing out the content key lookup, or failing to get the elements of the content array, yields an array of one object rather than the object itself. And you can't index an array with a string.




          Assuming you want CSV output:



          $ jq -r '.content[] | .details.Temperature[] as $t | [.Title,.brand,$t.value,$t.characteristics] | @csv' file.json
          "abc","xyz","90","Normal"
          "abc","xyz","100","high"
          "abc","xyz","80","low"


          The <object(s)> as <variable> acts like a loop in jq, so what happens here is that $t will be assigned each element of .details.Temperature[] in turn, and for each element, a new array is constructed. The array is passed through @csv which will output CSV-formatted rows.



          jq will always double quote the fields of its CSV output. To get rid of unnecessary quotes:



          jq -r '...as above...' file.json | csvformat


          (csvformat is part of csvkit)



          Or, you may want to use @tsv in place of @csv to get tab-delimited output instead.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 9 hours ago

























          answered 9 hours ago









          KusalanandaKusalananda

          136k17256424




          136k17256424







          • 2





            Kusal Thank you for your inputs. I am using above thing with for loop. details below. for field in Temperature certifications ; do echo $field :: jq --arg field "$field" -r ' .content[] | .details."$field"[] as $t | [.Title,.brand,$t.value,$t.characteristics] | @csv' file.json done. but getting "jq: error: try .["field"] instead of .field for unusually named fields at <top-level>, line 1:"

            – sam
            8 hours ago











          • I got this, I was not able to correlate and was putting .details.$[field] or .details."$field". Now I have changed that to .details[$field][] and it's working fine now.

            – sam
            7 hours ago











          • @sam Sorry, I was elsewhere. Yes, .details[$field][] or .["details"][$field][] is the correct syntax.

            – Kusalananda
            7 hours ago











          • I have up-voted and thank you for your support.

            – sam
            1 hour ago












          • 2





            Kusal Thank you for your inputs. I am using above thing with for loop. details below. for field in Temperature certifications ; do echo $field :: jq --arg field "$field" -r ' .content[] | .details."$field"[] as $t | [.Title,.brand,$t.value,$t.characteristics] | @csv' file.json done. but getting "jq: error: try .["field"] instead of .field for unusually named fields at <top-level>, line 1:"

            – sam
            8 hours ago











          • I got this, I was not able to correlate and was putting .details.$[field] or .details."$field". Now I have changed that to .details[$field][] and it's working fine now.

            – sam
            7 hours ago











          • @sam Sorry, I was elsewhere. Yes, .details[$field][] or .["details"][$field][] is the correct syntax.

            – Kusalananda
            7 hours ago











          • I have up-voted and thank you for your support.

            – sam
            1 hour ago







          2




          2





          Kusal Thank you for your inputs. I am using above thing with for loop. details below. for field in Temperature certifications ; do echo $field :: jq --arg field "$field" -r ' .content[] | .details."$field"[] as $t | [.Title,.brand,$t.value,$t.characteristics] | @csv' file.json done. but getting "jq: error: try .["field"] instead of .field for unusually named fields at <top-level>, line 1:"

          – sam
          8 hours ago





          Kusal Thank you for your inputs. I am using above thing with for loop. details below. for field in Temperature certifications ; do echo $field :: jq --arg field "$field" -r ' .content[] | .details."$field"[] as $t | [.Title,.brand,$t.value,$t.characteristics] | @csv' file.json done. but getting "jq: error: try .["field"] instead of .field for unusually named fields at <top-level>, line 1:"

          – sam
          8 hours ago













          I got this, I was not able to correlate and was putting .details.$[field] or .details."$field". Now I have changed that to .details[$field][] and it's working fine now.

          – sam
          7 hours ago





          I got this, I was not able to correlate and was putting .details.$[field] or .details."$field". Now I have changed that to .details[$field][] and it's working fine now.

          – sam
          7 hours ago













          @sam Sorry, I was elsewhere. Yes, .details[$field][] or .["details"][$field][] is the correct syntax.

          – Kusalananda
          7 hours ago





          @sam Sorry, I was elsewhere. Yes, .details[$field][] or .["details"][$field][] is the correct syntax.

          – Kusalananda
          7 hours ago













          I have up-voted and thank you for your support.

          – sam
          1 hour ago





          I have up-voted and thank you for your support.

          – sam
          1 hour ago

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f506789%2ferror-message-cannot-index-array-with-string-title-when-parsing-json-data-wi%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          瀋陽號驅逐艦 目录 接收與服役 配置反潛直升機 武進三型性能升級 歷史 除役 參考資料 外部連結 导航菜单Taiwan Air Power海疆老兵-陽字號驅逐艦沿革World Navies Today: Taiwan (Republic of China)DD-839 USS POWER

          波兰旗帜列表 目录 国旗 军旗 其他制服部门旗帜 特别国家机构船只 参考文献 外部链接 导航菜单Polskie flagi, chorągwie, bandery... [波兰旗帜、条幅、船旗等]原始内容Ustawa z dnia 31 stycznia 1980 r. o godle, barwach i hymnie Rzeczypospolitej Polskiej oraz o pieczęciach państwowychZarządzenie Ministra Obrony Narodowej z dnia 14 grudnia 2005 r. zmieniające zarządzenie w sprawie szczegółowych zasad używania znaków Sił Zbrojnych Rzeczypospolitej Polskiej oraz ustalenia innych znaków używanych w Siłach Zbrojnych Rzeczypospolitej PolskiejZarządzenie Ministra Obrony Narodowej z dnia 29 stycznia 1996 r. w sprawie szczegółowych zasad używania znaków Sił Zbrojnych Rzeczypospolitej Polskiej oraz ustalenia innych znaków używanych w Siłach Zbrojnych Rzeczypospolitej PolskiejUstawa z dnia 19 lutego 1993 r. o znakach Sił Zbrojnych Rzeczypospolitej PolskiejHistoria Marynarki Wojennej RP [波兰海军史]Rozporządzenie Ministra Spraw Wewnętrznych i Administracji z dnia 12 kwietnia 2002 r. w sprawie wzoru flagi oraz oznakowania jednostek pływających i statków powietrznych Straży GranicznejRozporządzenie Ministra Spraw Wewnętrznych i Administracji z dnia 18 kwietnia 2005 r. w sprawie wzoru flagi oraz oznakowania jednostek pływających i statków powietrznych PolicjiRozporządzenie Ministra Infrastruktury z dnia 21 października 2005 r. w sprawie wzorów flag dla statków morskich na oznaczenie pełnionej specjalnej służby państwowej oraz okoliczności i warunków ich podnoszenia波兰旗帜波兰

          Indenting and Dedenting ASP code with Python