When parsing a json response using jq run from cmd.exe on a Windows 10 machine, I always get either a null response or an error when trying to get a value present in the JSON.
Here is the curl:
curl -X POST -H “Content-Type: application/json” -H “Accept: application/json” -H “Accept-Encoding: gzip, deflate” –compressed -d “{“upc”: “4002293401102”}” “https://api.upcitemdb.com/prod/trial/lookup” | jq “.ean”
Here is the JSON response. For brevity, I shortened the full response as it’s quite long.
{
“code”: “OK”,
“total”: 1,
“offset”: 0,
“items”: [
{
“ean”: “4002293401102”,
“title”: “Wusthof Gourmet 3-Inch Serrated Paring Knife”,
“asin”: “B0000DJYE3”,
“elid”: “192088094351”
}
]
}
Here is what I’ve tried in jq to obtain the EAN value. In parenthesis I put abbreviated errors I receive.
- jq “.items.ean” (I get: jq: error (at :0): Cannot index array with string “ean”)
- jq “[items].ean” (I get: curl: Failed writing body)
- jq “.[items].ean” (I get: curl: Failed writing body)
- jq “[].items.ean” (I get: jq: error (at :0): Cannot index array with string “items”)
- jq “[].[items].ean” (I get: curl: Failed writing body)
- jq “.items.[ean]” (I get: curl: Failed writing body”
I’m sure I’m using the wrong syntax for jq. Any ideas what I’m doing wrong? I just cannot seem to get the syntax of getting that value.
Michael Steed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.