JQ get data from an array of array - json

How can I get a value from an array of arrays. This is JSON from which I have to get the data:
{
"status": "success",
"data": {
"resultType": "matrix",
"result": [
{
"metric": {
"myMetric": "ABC"
},
"values": [
[
1633597734,
"64.54166666666667"
],
[
1633598034,
"65.51666666666667" <-- wanted value
]
]
}
]
}
}
I have to get value from the last array in values.
I tried the following:
jq .data.result.values input.json
jq .data.result.values[] input.json
jq .data.result.values[][] input.json
For every one the result is:
jq: error (at json:0): Cannot index array with string "values"
How can I get value from the last array in values?

You can use a negative index to enumerate from the back of an array: to get the last element of the last array in the values of the last element of result:
.data.result[-1].values[-1][-1]

demo: https://jqplay.org/s/YMpB8-A4-Q
filter:
.data.result[].values[1][1]?
input:
{
"status": "success",
"data": {
"resultType": "matrix",
"result": [
{
"metric": {
"myMetric": "ABC"
},
"values": [
[
1633597734,
"64.54166666666667"
],
[
1633598034,
"65.51666666666667"
]
]
}
]
}
}
output:
"65.51666666666667"

Related

How to check if json contains another json using jq?

I have just started using jq and I need to check if a given json is present in another json using jq?
Suppose this is my json_input:
{
"info": {
"values": [
{
"data": [
{
"name": "name",
"value": "val"
}
]
}
]
}
}
I need to check if the above json input is present inside the following available_json:
{
"info": {
"values": [
{
"data": [
{
"name": "name",
"value": "val"
},
{
"name": "name2",
"value": "val2"
},
{
"name": "name3",
"value": "val3"
}
],
"key1":"val1",
"key2":"val2"
}
],
"priority":1,
"objects":[
{
"name":"a"}
]
}
}
Both json are stored in variables and should report the presence for any json_input given as input based on any available_json (generic). How can this be done using jq?
Or Is there any other better way like converting both json to string and then comparing?
PS: The json object key info is fixed and the values can change.
This is so trivial that one might not even think of it: Using the jq filter contains:
jq 'contains({
"info": {
"values": [
{
"data": [
{
"name": "name",
"value": "val"
}
]
}
]
}
})' available.json
Output will be true or false. If you run jq -e (--exit-status), it will set the exit status accordingly, which allows you to use it together with if or &&/|| in your shell.
If you the input_json is also stored in a file:
jq --slurpfile input_json input_json 'contains($input_json[0])' available.json
If the JSON document is stored in a variable, then --argjson instead of --slurpfile:
jq --argjson input_json "$input_json" 'contains($input_json)' available.json
or simply relying on parameter expansion of your shell:
jq "contains($input_json)" available.json

Combining all key value pairs in one using jq filter or jq play

I want to transform JSON data using jq filter
Json data:
{
"main": [
{
"firstKey": "ABCD",
"id": "12345",
"data": [
{
"name": "first_id",
"value": "first_id_value"
},
{
"name": "second_id",
"value": "second_id_value"
},
{
"name": "third_id",
"value": "third_id_value"
}
]
}
]
}
Expected OUTPUT:
{
"firstKey": "ABCD",
"id": "12345",
"data.name.first_id": "first_id_value",
"data.name.second_id": "second_id_value",
"data.name.third_id": "third_id_value"
}
After many trials and errors, I was near to expected output using following filter expression
[.main[]|{"firstKey", "id"},foreach .data[] as $item (0; "data.name.\($item.name)" as $a|$item.value as $b| {($a): $b})][]
Used foreach as objects under "data" are dynamic. the number of objects can differ.
The output for the above expression is:
{
"firstKey": "ABCD",
"id": "12345"
}
{
"data.name.first_id": "first_id_value"
}
{
"data.name.second_id": "second_id_value"
}
{
"data.name.third_id": "third_id_value"
}
But I want the objects of data to be under the same braces as 'firstKey' and 'id'.
LINK to JqPlay
Any suggestions will be helpful.
Since your structure is so rigid, you can cheat and use the built-in from_entries, which takes a list of {key, value} pairs and constructs an object:
.main[] |
{firstKey, id} +
(.data | map({key: "data.name.\(.name)", value}) |
from_entries)

Flatten JSON array of objects that contain an array with Jolt

I'm using Jolt 0.1.0, and trying to transform the following JSON:
{
"records": [
{
"collectionId": "COLLECTION1",
"recordIds": [
"recA",
"recB"
]
},
{
"collectionId": "COLLECTION1",
"recordIds": [
"recC",
"recD",
"recE"
]
},
{
"collectionId": "COLLECTION2",
"recordIds": [
"recF",
"recG"
]
}
]
}
... to this:
{
"records": [
"COLLECTION1:recA",
"COLLECTION1:recB",
"COLLECTION1:recC",
"COLLECTION1:recD",
"COLLECTION1:recE",
"COLLECTION2:recF",
"COLLECTION2:recG"
]
}
I've made several attempts with the modify-default-beta operator and the concat function, but can't seem to make it work.
Very similar to https://github.com/bazaarvoice/jolt/issues/656 which required 4 steps.

jmespath flatten multiple hash values

Ideally, I want to write a query that returns a flat list output: ["abc", "bcd", "cde", "def"] from the following JSON sample:
{
"l_l": [
[1,2,3],
[4,5,6]
],
"l_h_l": [
{ "n": [10,2,3] },
{ "n": [4,5,60] }
],
"l_h_m": [
{
"n": {
"1234": "abc",
"2345": "bcd"
}
}, {
"n": {
"3456": "cde",
"4567": "def"
}
}
]
}
The closest I can get is l_h_m[].n.* which returns the contents that I want as an unflattened list of lists:
[
[
"abc",
"bcd"
],
[
"cde",
"def"
]
]
jmespath lets you flatten lists of lists. Queries l_l[] and l_h_l[].n[] both returned flattened results, when the source json is structured that way.
Looks like your solution just required another flattening operator.
l_h_m[].n.*[]
returns
[
"abc",
"bcd",
"cde",
"def"
]

Create one JSON from another, with extra data va JQ

I have this file:
[
"smoke-tests",
"push-apps-manager"
]
I'd like to get this output using JQ:
{
"errands": [
{"name": "smoke-tests", "post_deploy": true},
{"name": "push-apps-manager", "post_deploy": true}
]
}
It seems so simple, yet, I have so much difficulty here...
It's a little tricky, since you need to embed the input into the list bound to the errands key. Start by creating the sequence of name/post_deploy objects:
% jq '.[] | {name: ., post_deploy: true}' names.json
{
"name": "smoke-tests",
"post_deploy": true
}
{
"name": "push-apps-manager",
"post_deploy": true
}
Then wrap that in the list in the outer object:
% jq '{errands: [.[] | {name: ., post_deploy: true}]}' names.json
{
"errands": [
{
"name": "smoke-tests",
"post_deploy": true
},
{
"name": "push-apps-manager",
"post_deploy": true
}
]
}
You can also use the map function (which I rarely remember how to use correctly, but it turns out is pretty simple here):
% jq '{errands: map({name:., post_deploy: true})}' names.json
Here is another approach. If you are new to jq it may be easiest to work towards the goal in small steps.
1) Start with the identity filter
.
which produces as expected
[
"smoke-tests",
"push-apps-manager"
]
2) next add the outer object with the "errands" key:
{ "errands": . }
which produces
{
"errands": [
"smoke-tests",
"push-apps-manager"
]
}
3) next move the data into an array
{ "errands": [ . ] }
which produces
{
"errands": [
[
"smoke-tests",
"push-apps-manager"
]
]
}
4) add the inner object with the "name" and "post_deploy" keys
{ "errands": [ { "name": ., "post_deploy": true } ] }
which produces
{
"errands": [
{
"name": [
"smoke-tests",
"push-apps-manager"
],
"post_deploy": true
}
]
}
5) Now we're really close. All we need to do is take advantage of jq's Object Construction behavior when an expression produces multiple results :
{ "errands": [ { "name": .[], "post_deploy": true } ] }
which gives us the desired result
{
"errands": [
{
"name": "smoke-tests",
"post_deploy": true
},
{
"name": "push-apps-manager",
"post_deploy": true
}
]
}