Can't use JQ to get items within JSON - json

I have this JSON:
{
"blocks" : {
"xrb_1111111111111111111111111111111111111111111111111117353trpda": {
"142A538F36833D1CC78B94E11C766F75818F8B940771335C6C1B8AB880C5BB1D": "6000000000000000000000000000000"
},
"xrb_3t6k35gi95xu6tergt6p69ck76ogmitsa8mnijtpxm9fkcm736xtoncuohr3": {
"4C1FEEF0BEA7F50BE35489A1233FE002B212DEA554B55B1B470D78BD8F210C74": "106370018000000000000000000000000"
}
}
I am having trouble using jq in bash to read out:
xrb_1111111111111111111111111111111111111111111111111117353trpda
xrb_3t6k35gi95xu6tergt6p69ck76ogmitsa8mnijtpxm9fkcm736xtoncuohr3
I try to do it on jqplay but not having much luck with it.
I have tried with jqplay to get the results and manage find but not when its nested this way.

This jq script might work for you:
$ cat file
{
"blocks": {
"foo_b": {
"aaa": "bbb"
},
"bar_b": {
"ccc": "ddd"
}
}
}
$ jq -r '.blocks | to_entries[].key' file
foo_b
bar_b

keys_unsorted gives an array of keys. Whence:
jq -r '.blocks | keys_unsorted[]' input.json
xrb_1111111111111111111111111111111111111111111111111117353trpda
xrb_3t6k35gi95xu6tergt6p69ck76ogmitsa8mnijtpxm9fkcm736xtoncuohr3

First of all, your JSON is invalid. It's missing a closing bracket.
$ cat input.json
{
"blocks": {
"xrb_1111111111111111111111111111111111111111111111111117353trpda": {
"142A538F36833D1CC78B94E11C766F75818F8B940771335C6C1B8AB880C5BB1D": "6000000000000000000000000000000"
},
"xrb_3t6k35gi95xu6tergt6p69ck76ogmitsa8mnijtpxm9fkcm736xtoncuohr3": {
"4C1FEEF0BEA7F50BE35489A1233FE002B212DEA554B55B1B470D78BD8F210C74": "106370018000000000000000000000000"
}
}
}
If you're open to use something else than jq, then I'd suggest the Swiss knife tool Xidel.
Dot notation:
$ xidel -s input.json -e '($json).blocks()'
XPath notation:
$ xidel -s input.json -e '$json/(blocks)()'
Both with the output:
xrb_1111111111111111111111111111111111111111111111111117353trpda
xrb_3t6k35gi95xu6tergt6p69ck76ogmitsa8mnijtpxm9fkcm736xtoncuohr3

Related

jq to diff json in bash

I have 2 json objects comming from a rest api. I want to compare if they are the same object.
objectA:
{
"type": {
"S": "equal"
},
"preFilter": {
"BOOL": true
}
}
objectB:
{
"preFilter": {
"BOOL": true
},
"type": {
"S": "equal"
}
}
They are the same, but an md5sum will see them as different. I tried inserting them in 2 different files, and compare the files using something proposed
here: but I would like to know if it's possible to use jq on the fly to compare variables.
I've been trying to change
--argfile a a.json
for
--arg a $a
(being $a a json string) with no luck. Any idea how to approach strings, not files?
It would probably be simplest to use the --argjson command-line option, e.g.
jq -n --argjson a "$a" --argjson b "$b" '$a == $b'
Of course there are alternatives, e.g. using jq -s ...

JQ Join JSON files by key

Looks like it's not actual for jq 1.4, could you provide any other ways to join JSON files by key?
e.g
{
"key": "874102296-1",
"que_lat": "40"
}
{
"key": "874102296-2",
"que_lat": "406790"
}
and
{
"key": "874102296-1",
"in_time": "1530874104733",
"latency": "12864258288242"
}
{
"key": "874102296-2",
"in_time": "1530874104746"
}
As a result, i'd like to have something like this:
{
"key": "874102296-1",
"in_time": "1530874104733",
"full_latency": "12864258288242",
"que_lat": "40"
}
{
"key": "874102296-2",
"in_time": "1530874104746",
"que_lat": "406790"
}
Thanks!
The problem can easily be solved using the def of hashJoin given in the SO page that you cite.
Solution using jq 1.5 or higher
If you have jq 1.5 or higher, you could use this invocation:
jq -n --slurpfile f1 file1.json --slurpfile f2 file2.json -f join.jq
where join.jq contains the second def of hashJoin, together with:
hashJoin($f1; $f2; .key)[]
Solution using jq 1.4
If you have jq 1.4, the trickiness is to read each of the two files separately as an array. Here's one approach that assumes bash:
jq -n --argfile f1 <(jq -s . file1.json) --argfile f2 <(jq -s . file2.json) -f join.jq
where join.jq is as above.
If you cannot use bash, then it might be simplest to create temporary files.

Getting output using jq

I have the following JSON outp
{
"environment": {
"reg": "abc"
},
"system": {
"svcs": {
"upsvcs": [
{
"name": "monitor",
"tags": [],
"vmnts": [],
"label": "upsvcs",
"credentials": {
"Date": "Feb152018",
"time": "1330"
}
},
{
"name": "application",
"tags": [],
"vmnts": [],
"label": "upsvcs",
"credentials": {
"lastViewed": "2018-02-07"
}
}
]
}
}
and to retrieve Date value (from credentials). I have tried `curl xxx | jq -r '. | select (.Date)'
which is not returning any value. Can someone please let me know what is the correct syntax and any explanation on how to retrieve elements (or any articles that do so).
TIA
The (or at least a) short answer is:
.system.svcs.upsvcs[0].credentials.Date
Without a schema, it is often a bit tricky to get the exact path correctly, so you might want to consider using the jq filter paths. In your case:
$ jq -c paths input.json | grep Date
["system","svcs","upsvcs",0,"credentials","Date"]
You could also use this path (i.e., the above array) directly:
$ jq 'getpath(["system","svcs","upsvcs",0,"credentials","Date"])' input.json
"Feb152018"
and thus you could use a "path-free" query:
$ jq --argjson p $(jq -c paths input.json | grep --max 1 Date) 'getpath($p)' input.json
"Feb152018"
Another approach would be just to retrieve all “truthy” .Date values, no matter where they appear:
$ jq -c '.. | .Date? // empty' input.json
"Feb152018"
select
Since you mentioned select, please note that:
$ jq -c '.. | select(.Date?)' input.json
{"Date":"Feb152018","time":"1330"}
Further information
For further information about jq in general and the topic of retrieval in particular, see the online tutorial, manual, and FAQ:
https://stedolan.github.io/jq/tutorial/
https://stedolan.github.io/jq/manual/v1.5/
https://github.com/stedolan/jq/wiki/FAQ

un-prettify JSON in bash

I want to use a "prettified" JSON with a bash cat argument. Due to its used with another function I need to have it "un-prettified" (all white spaces and linebreaks removed). Is there a simple way to do it?
To give you a simple example its like this:
Working
file.json:
{ "name":"John", "age":30, "city":"New York"}
foo.sh:
#!/bin/sh
myfunc cat file.json //..
Not Working
file.json:
{
"name":"John",
"age":30,
"city":"New York"
}
foo.sh:
#!/bin/sh
myfunc cat file.json //..
Any suggestions?
You could use jq with -c parameter
jq -c '.' file.json
will print
{"name":"John","age":30,"city":"New York"}
Probably jq could help you.
file.json
{
"name": "John",
"age": 30,
"city": "New York"
}
Do:
cat file.json | jq -c
Will return:
{"name":"John","age":30,"city":"New York"}
Even better as #Charles Duffy comment to only spin up one process, not two:
jq -c < file.json

Create JSON file using jq

I'm trying to create a JSON file by executing the following command:
jq --arg greeting world '{"hello":"$greeting"}' > file.json
This command stuck without any input. While
jq -n --arg greeting world '{"hello":"$greeting"}' > file.json
doesn't parse correctly. I'm just wondering is really possible to create a JSON file.
So your code doesn't work because included the variable inside double quotes which gets treated as string. That is why it is not working
As #Jeff Mercado, pointed out the solution is
jq -n --arg greeting world '{"hello":$greeting}' > file.json
About the - in a name. This is actually possible. But as of now this is not available in released version of jq. If you compile the master branch of jq on your system. There is a new variable called $ARGS.named which can be used to access the information.
I just compiled and check the below command and it works like a charm
./jq -n --arg name-is tarun '{"name": $ARGS.named["name-is"]}'
{
"name": "tarun"
}
$ARGS provides access to named (--arg name value) and positional (--args one two three) arguments from the jq command line, and allows you to build up objects easily & safely.
Named arguments:
$ jq -n '{christmas: $ARGS.named}' \
--arg one 'partridge in a "pear" tree' \
--arg two 'turtle doves'
{
"christmas": {
"one": "partridge in a \"pear\" tree",
"two": "turtle doves"
}
}
Positional arguments:
$ jq -n '{numbers: $ARGS.positional}' --args 1 2 3
{
"numbers": [
"1",
"2",
"3"
]
}
Note you can access individual items of the positional array, and that the named arguments are directly available as variables:
jq -n '{first: {name: $one, count: $ARGS.positional[0]}, all: $ARGS}' \
--arg one 'partridge in a "pear" tree' \
--arg two 'turtle doves' \
--args 1 2 3
{
"first": {
"name": "partridge in a \"pear\" tree",
"count": "1"
},
"all": {
"positional": [
"1",
"2",
"3"
],
"named": {
"one": "partridge in a \"pear\" tree",
"two": "turtle doves"
}
}
}
To add to what Jeff and Tarun have already said, you might want to use the \() string interpolation syntax in your command. eg.
jq -n --arg greeting world '{"hello":"\($greeting)"}'
for me this produces
{
"hello": "world"
}
Regarding your reply to Jeff's comment, the argument name you choose has to be a valid jq variable name so an arg like greeting-for-you won't work but you could use underscores so greeting_for_you would be ok. Or you could use the version Tarun described.