Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jq - Cannot index string with string

Tags:

shell

jq

The content is

{
  "properties" : {
    "CloudSanityPassed" : [ "true" ],
    "GITCOMMIT" : [ "test1" ],
    "buildNumber" : [ "54" ],
    "jobName" : [ "InveergDB-UI" ]
  },
  "uri" : "http://ergctory:8081/aergergory/api/storage/test-reergerglease-reergpo/cergom/cloergud/waf/ergregBUI/1ergerggregSHOT/ergregerg-34.zip"
}

I use this command

.[] | ."CloudSanityPassed" | .[]

And I get this message

jq: error (at <stdin>:8): Cannot index string with string "CloudSanityPassed"
"true"
exit status 5

I get, what I want ("true" value), but there is a error in output. Could you explain me, how to avoid it and why does it happen?

like image 282
dice2011 Avatar asked May 08 '17 08:05

dice2011


2 Answers

According to the jq manual, .[] gets the values of the object when applied to object.

So you get two objects, one for value of "properties" and another for value of "uri":

{
  "CloudSanityPassed": [
    "true"
  ],
  "GITCOMMIT": [
    "test1"
  ],
  "buildNumber": [
    "54"
  ],
  "jobName": [
    "InveergDB-UI"
  ]
}
"http://ergctory:8081/aergergory/api/storage/test-reergerglease-reergpo/cergom/cloergud/waf/ergregBUI/1ergerggregSHOT/ergregerg-34.zip"

jq tries to apply ."CloudSanityPassed" operator to each object.

Since former object is dictionary (aka hash), you can apply ."CloudSanityPassed" and get the value ["true"], however, latter is an simple string which you cannot apply ."CloudSanityPassed", so jq outputs error here.

Maybe the command you want is just .properties.CloudSanityPassed.

like image 123
ymonad Avatar answered Sep 18 '22 22:09

ymonad


In my case jq '[.[] | group_by(.foo)]' gave the error but jq '[.[]] | group_by(.foo)' worked

like image 37
Eric B Avatar answered Sep 19 '22 22:09

Eric B