Retrieve value of #odata.id with jq in Redfish output - json

I'm parsing some json Redfish data using jq.
Trying to pull value for #odata.id from redfish.txt file below.
Invoking with recommended jq .["#odata.id"] doesn't seem to quite work to pull just value itself which is: /redfish/v1/Systems
Any suggestions welcomed. Output below... :)
Thanks,
Nick
root#ubuntu-xenial:/var/opt# cat redfish.txt
{"#odata.context":"/redfish/v1/$metadata#ServiceRoot.ServiceRoot","#odata.id":"/redfish/v1","#odata.type":"#ServiceRoot.v1_2_0.ServiceRoot","AccountService":{"#odata.id":"/redfish/v1/Managers/iDRAC.Embedded.1/AccountService"},"Chassis":{"#odata.id":"/redfish/v1/Chassis"},"Description":"Root Service","EventService":{"#odata.id":"/redfish/v1/EventService"},"Id":"RootService","JsonSchemas":{"#odata.id":"/redfish/v1/JSONSchemas"},"Links":{"Sessions":{"#odata.id":"/redfish/v1/Sessions"}},"Managers":{"#odata.id":"/redfish/v1/Managers"},"Name":"Root Service","Oem":{"Dell":{"#odata.type":"#DellServiceRoot.v1_0_0.ServiceRootSummary","IsBranded":0,"ManagerMACAddress":"d0:96:69:51:d4:70","ServiceTag":"XXXX"}},"RedfishVersion":"1.2.0","Registries":{"#odata.id":"/redfish/v1/Registries"},"SessionService":{"#odata.id":"/redfish/v1/SessionService"},"Systems":{"#odata.id":"/redfish/v1/Systems"},"Tasks":{"#odata.id":"/redfish/v1/TaskService"},"UpdateService":{"#odata.id":"/redfish/v1/UpdateService"}}
root#ubuntu-xenial:/var/opt# cat redfish.txt | jq .Systems
{
"#odata.id": "/redfish/v1/Systems"
}
root#ubuntu-xenial:/var/opt# cat redfish.txt | jq .Systems | jq .#odata.id
jq: error: syntax error, unexpected FIELD, expecting QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
.#odata.id
jq: error: try .["field"] instead of .field for unusually named fields at <top-level>, line 1:
.#odata.id
jq: 2 compile errors
root#ubuntu-xenial:/var/opt# cat redfish.txt | jq .Systems | jq .["#odata.id"]
{
"#odata.id": "/redfish/v1/Systems"
}
"/redfish/v1/Systems"
root#ubuntu-xenial:/var/opt# cat redfish.txt | jq .Systems | jq .["odata.id"]
{
"#odata.id": "/redfish/v1/Systems"
}
"/redfish/v1/Systems"
root#ubuntu-xenial:/var/opt#

You could simply use the filter:
.Systems["#odata.id"]
That is, at a bash or bash-like prompt, you'd type something like:
jq '.Systems["#odata.id"]' redfish.txt

i stumbled across this, as this is the highest rated site for a duckduckgo query of "jq" and "#odata.id"
Unfortunately the answer is incorrect ... after some messing around I have got the correct answer:
jq '.Systems.[]"#odata.id"'
Since the things in the square brackets [] are an array that are a 0 indexed (start at zero) numeric array, you'll get an error trying to reference an element called "#odata.id" (i.e. .Systems["#odata.id"]), while .Systems[1] would work (if there are two elements in the array)
If you want all the elements ... use the above, if you want a specific element, you can use:
jq '.Systems[#]."odata.id"'
There should be an earlier component (in the output) that will tell you how many elements (hopefully), something like "Members#odata.count"
BUT ... after i typed this and checked your data i realized i was solving my case, that looked like yours, but wasn't. In actually looking at your data, what you need is ...
jq '.Systems."#odata.id"'
Because you DON'T have an array, you have have normal key that just needs proper quoting ...
Hopefully this helps

Related

Select multiple values from a split string in JQ

I see questions about selecting multiple values from an array using JQ, but I have a string that originally I just need the value after the last /, which is easily selected:
Input:
https://www.googleapis.com/compute/v1/projects/test-project-1/zones/europe-west1-b/instanceGroups/test-instance-group-1
JQ:
jq -r '.[]|.zone|=split("/")[-1]|"\(.name) \(.zone)"'
Output:
test-instance-group-1 europe-west1-b
However for the actual instances, the zone isn't listed, so must be extracted from the same key instance.
Input:
https://www.googleapis.com/compute/v1/projects/test-project-1/zones/europe-west1-b/instances/test-instance-1
JQ:
jq -r '.[]|.instance|=split("/")[-1]|"\(.instance)'
Output:
test-instance-1
However, I also want to extract the zone infomation, from the input as well, which I presume is selected with =split("/")[-3] however no matter how I format the request to JQ, I get errors:
$ jq -r '.[]|.instance|=split("/")[-1][-3]|"\(.instance)"'
jq: error (at <stdin>:47): Cannot index string with number
How can I extract two strings, from the same value/key ?
You're looking for something like this:
jq -r '.[].instance | split("/") | "\(.[-1]) \(.[-3])"'

Jq parse error: Invalid numeric literal at line 1, column 9

I am trying to get values from a json file from a url using curl and then printing specific keys with jq command (e.g. company). Unfortunately when I use:
jq '.[] | .company' JB.json
I get the error:
parse error: Invalid numeric literal at line 1, column 9. I have checked the downloaded file with less and it looks exactly like in the url.
Some people suggested to use the -R option but it prints:
jq: error: Cannot iterate over string
The url of the file is: https://jobs.github.com/positions.json?description=python&location=new+york
If you use curl -sS to retrieve the file, the '//'-style comments are skipped:
curl -Ss 'https://jobs.github.com/positions.json?description=python&location=new+york' | jq '.[] | .company'
"BentoBox"
"Aon Cyber Solutions"
"Sesame"
"New York University"
So presumably your JB.json contains the "//"-style comments. The simplest workaround would probably be to filter out those first two lines (e.g. using sed (or jq!)) first.
Here's a jq-only solution:
< JB.json jq -Rr 'select( test("^//")|not)' |
jq '.[] | .company'

How to pass a key to a jq file

I would like to write a simple jq file that allows me to count items grouped by a specified key.
I expect the script contents to be something similar too:
group_by($group) | map({group: $group, cnt: length})
and to invoke it something like
cat my.json | jq --from-file count_by.jq --args group .header.messageType
Whatever I've tried the argument always ends up as a string and is not usable as a key.
Since you have not followed the minimal complete verifiable example
guidelines, it's a bit difficult to know what the best approach to your problem will be, but whatever approach you take, it is important to bear in mind that --arg always passes in a JSON string. It cannot be used to pass in a jq program fragment unless the fragment is a JSON string.
So let's consider one option: passing in a JSON object representing a path that you can use in your program.
So the invocation could be:
jq -f count_by.jq --argjson group '["header", "messageType"]'
and the program would begin with:
group_by(getpath($group)) | ...
Having your cake ...
If you really want to pass in arguments such as .header.messageType, there is a way: convert the string $group into a jq path:
($group|split(".")|map(select(length>0))) as $path
So your jq filter would look like this:
($group|split(".")|map(select(length>0))) as $path
| group_by(getpath($path)) | map({group: $group, cnt: length})
Shell string interpolation
If you want a quick bash solution that comes with many caveats:
group=".header.messageType"
jq 'group_by('"$group"') | map({group: "'"$group"'", cnt: length}'

I cannot get jq to give me the value I'm looking for.

I'm trying to use jq to get a value from the JSON that cURL returns.
This is the JSON cURL passes to jq (and, FTR, I want jq to return "VALUE-I-WANT" without the quotation marks):
[
{
"success":{
"username":"VALUE-I-WANT"
}
}
]
I initially tried this:
jq ' . | .success | .username'
and got
jq: error (at <stdin>:0): Cannot index array with string "success"
I then tried a bunch of variations, with no luck.
With a bunch of searching the web, I found this SE entry, and thought it might have been my saviour (spoiler, it wasn't). But it led me to try these:
jq -r '.[].success.username'
jq -r '.[].success'
They didn't return an error, they returned "null". Which may or may not be an improvement.
Can anybody tell me what I'm doing wrong here? And why it's wrong?
You need to pipe the output of .[] into the next filter.
jq -r '.[] | .success.username' tmp.json
tl;dr
# Extract .success.username from ALL array elements.
# .[] enumerates all array elements
# -r produces raw (unquoted) output
jq -r '.[].success.username' file.json
# Extract .success.username only from the 1st array element.
jq -r '.[0].success.username' file.json
Your input is an array, so in order to access its elements you need .[], the array/object-value iterator (as the name suggests, it can also enumerate the properties of an object):
Just . | sends the input (.) array as a whole through the pipeline, and an array only has numerical indices, so the attempt to index (access) it with .success.username fails.
Thus, simply replacing . | with .[] | in your original attempt, combined with -r to get raw (unquoted output), should solve your problem, as shown in chepner's helpful answer.
However, peak points out that since at least jq 1.3 (current as of this writing is jq 1.5) you don't strictly need a pipeline, as demonstrated in the commands at the top.
So the 2nd command in your question should work with your sample input, unless you're using an older version.

Ignore Unparseable JSON with jq

I'm using jq to parse some of my logs, but some of the log lines can't be parsed for various reasons. Is there a way to have jq ignore those lines? I can't seem to find a solution. I tried to use the --seq argument that was recommended by some people, but --seq ignores all the lines in my file.
Assuming that each log entry is exactly one line, you can use the -R or --raw-input option to tell jq to leave the lines unparsed, after which you can prepend fromjson? | to your filter to make jq try to parse each line as JSON and throw away the ones that error.
I have log stream where some messages are in json format.
I want to pipe the json messages through jq, and just echo the rest.
The json messages are on a single line.
Solution: use grep and tee to split the lines in two streams, those starting with "^{" pipe through jq and the rest just echo to terminal.
kubectl logs -f web-svjkn | tee >(grep -v "^{") | grep "^{" | jq .
or
cat logs | tee >(grep -v "^{") | grep "^{" | jq .
Explanation:
tee generates 2nd stream, and grep -v prints non json info, 2nd grep only pipes what looks like json opening bracket to jq.
This is an old thread, but here's another solution fully in jq. This allows you to both process proper json lines and also print out non-json lines.
jq -R . as $line | try (fromjson | <further processing for proper json lines>) catch $line'
There are several Q&As on the FAQ page dealing with the topic of "invalid JSON", but see in particular the Q:
Is there a way to have jq keep going after it hits an error in the input file?
In particular, this shows how to use --seq.
However, from the the sparse details you've given (SO recommends a minimal example be given), it would seem it might be better simply to use inputs. The idea is to process one JSON entity at a time, using "try/catch", e.g.
def handle: inputs | [., "length is \(length)"] ;
def process: try handle catch ("Failed", process) ;
process
Don't forget to use the -n option when invoking jq.
See also Processing not-quite-valid JSON.
If JSON in curly braces {}:
grep -Pzo '\{(?>[^\{\}]|(?R))*\}' | jq 'objects'
If JSON in square brackets []:
grep -Pzo '\[(?>[^\[\]]|(?R))*\]' | jq 'arrays'
This works if there are no []{} in non-JSON lines.