Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jq: error: Cannot iterate over string

Tags:

jq

could someone please explain below warning?

input file is:

{
 "env": "DC",
 "hosts" :
[
{
  "apt_update_last_success": "1495991703",
  "architecture": "amd64",
  "hostname": "h1"
},
{
  "apt_update_last_success": "1495991703",
  "architecture": "amd64",
  "hostname": "h2"
},
{
  "apt_update_last_success": "1496045706",
  "architecture": "amd64",
  "hostname": "h3"
},
{
  "apt_update_last_success": "1496045705",
  "architecture": "amd64",
  "hostname": "h4"
},
{
  "apt_update_last_success": "1496049305",
  "architecture": "amd64",
  "hostname": "h5"
},
{
  "apt_update_last_success": "1496049307",
  "architecture": "amd64",
  "hostname": "h6"
}
]
}

the jq command returns what expected but also prints the warning I dont know why:

$ jq -r '.[][] | select(.hostname=="h6")' ddd.json
jq: error: Cannot iterate over string
{
  "apt_update_last_success": "1496049307",
  "architecture": "amd64",
  "hostname": "h6"
}

please tell me how to get rid of this.

thank you.

like image 346
Chris Avatar asked Jun 24 '26 13:06

Chris


1 Answers

The issue is in your notation .[][]. Your input is just an object but you are trying to present it as a "container" of "containers" .[][].

The right way is:

jq '.hosts[] | select(.hostname=="h6")' ddd.json
{
  "apt_update_last_success": "1496049307",
  "architecture": "amd64",
  "hostname": "h6"
}

Besides, on jq 1.5 this jq -r '.[][] | select(.hostname=="h6")' ddd.json doesn't return the expected object but only prints jq: error (at jq1:36): Cannot iterate over string ("DC")

like image 199
RomanPerekhrest Avatar answered Jul 01 '26 11:07

RomanPerekhrest