Home > Mobile >  Searching for correct jq syntax for array of fruits
Searching for correct jq syntax for array of fruits

Time:02-23

i need some help about the jq with my given json structure.

    [
    { "Fruits" : [
      {
        "name": "appled",
        "color": "green",
        "price": 1.2
      },
      {
        "name": "bananad",
        "color": "yellow",
        "price": 0.5
      },
      {
        "name": "kiwid",
        "color": "green",
        "price": 1.25
      }
    ] }
    ]

jq '.Fruits[].name' fruits.json

jq: error (at fruits3.json:19): Cannot index array with string "Fruits"

I only want to have the Fruits like: "apple" "banana" "kiwi"

Example got from https://www.baeldung.com/linux/jq-command-json but i have one Array more in my given structure (reolink kamera)

CodePudding user response:

Your top level element is an array. You only want the first element of that from what I understand so you need a .[0] at the start. The total query would be .[0].Fruits[].name

If you instead want all inner objects just use .[] without an index instead so .[].Fruits[].name

CodePudding user response:

Thanks a lot! You guide me to in the right direction ;-)

Works for me (reolink search json index)

jq '.[].value.SearchResult.File[].name' index_41.json

http://192.168.0.41/cgi-bin/api.cgi?cmd=Search

[
   {
      "cmd" : "Search",
      "code" : 0,
      "value" : {
         "SearchResult" : {
            "File" : [
               {
                  "EndTime" : {
                     "day" : 22,
                     "hour" : 2,
                     "min" : 1,
                     "mon" : 2,
                     "sec" : 1,
                     "year" : 2022
                  },
                  "StartTime" : {
                     "day" : 22,
                     "hour" : 1,
                     "min" : 59,
                     "mon" : 2,
                     "sec" : 29,
                     "year" : 2022
                  },
                  "frameRate" : 0,
                  "height" : 0,
                  "name" : "Rec_20yymmdd_005929_541_M.mp4",
                  "size" : 63145751,
                  "type" : "main",
                  "width" : 0
               },
               {
                  "EndTime" : {
                     "day" : 22,
                     "hour" : 2,
                     "min" : 3,
                     "mon" : 2,
                     "sec" : 2,
                     "year" : 2022
                  },
                  "StartTime" : {
                     "day" : 22,
                     "hour" : 2,
                     "min" : 1,
                     "mon" : 2,
                     "sec" : 45,
                     "year" : 2022
                  },
                  "frameRate" : 0,
                  "height" : 0,
                  "name" : "Rec_20yymmdd_010145_541_M.mp4",
                  "size" : 28453395,
                  "type" : "main",
                  "width" : 0
...
  •  Tags:  
  • json
  • Related