I have a json file named as param.json that looks as below:
[
{
"Value": "anshuman.ceg+Dev#gmail.com",
"Key": "AccountEmail"
},
{
"Value": "DevABC",
"Key": "AccountName"
},
{
"Value": "Security (ou-nzx5-8ajd1561)",
"Key": "ManagedOrganizationalUnit"
},
{
"Value": "anshuman.ceg+Dev#gmail.com",
"Key": "SSOUserEmail"
},
{
"Value": "John",
"Key": "SSOUserFirstName"
},
{
"Value": "Smith",
"Key": "SSOUserLastName"
}
]
I want to get only the Value for DevABC so that I can use while reading the -r line. I need only DevABC
I am using jq as follows which doesn't seem to work
jq -r .[1].Value param.json
Assuming all your Key values are distinct, you can first convert the array into an object and then access the "AccountName" property directly:
jq -r 'from_entries | .AccountName' param.json
from_entries will generate the following object, which allows you to easily access the value for a given key:
{
"AccountEmail": "anshuman.ceg+Dev#gmail.com",
"AccountName": "DevABC",
"ManagedOrganizationalUnit": "Security (ou-nzx5-8ajd1561)",
"SSOUserEmail": "anshuman.ceg+Dev#gmail.com",
"SSOUserFirstName": "John",
"SSOUserLastName": "Smith"
}
If the object keys in the input happen not to be "Key" and "Value" and you can't use from_entries, select would be a good approach:
jq --arg k 'AccountName' -r '.[] | select(.Key == $k).Value'
Related
Here my document:
[
{
"id": "9f0e27fe-3b8f-4857-8e1d-e57e7a3f4c31",
"identifier": [
{
"system": {
"value": "urn:oid:1.3.6.1.4.1.19126.3"
},
"value": {
"value": "Y3454867M"
}
},
{
"system": {
"value": "urn:oid:2.16.724.4.9.10.2"
},
"value": {
"value": "108505134"
}
}
]
}
]
I need to pick only .identifier[where .system.value == "urn:oid:1.3.6.1.4.1.19126.3"] and project .identifier.value.value.
Desired output:
[
{
"id": "9f0e27fe-3b8f-4857-8e1d-e57e7a3f4c31",
"identifier": "Y3454867M"
}
]
I've been playing with map and select but I don't quite figure out what's the right way to get it.
Any ideas?
This approach uses first to get the first result, in case there is more than one array item matching the criteria.
jq --arg v "urn:oid:1.3.6.1.4.1.19126.3" '
map(.identifier |= first(.[] | select(.system.value == $v).value.value))
'
[
{
"id": "9f0e27fe-3b8f-4857-8e1d-e57e7a3f4c31",
"identifier": "Y3454867M"
}
]
Demo
Right on the money with the good ol' select tool, since you need data from an arbitrary index. I fumbled a bit before I unwrapped the inner array that gets piped to my select.
jq -r '.[] | [{id: .id, identifier: .identifier | .[] | select(.system.value | contains("urn:oid:1.3.6.1.4.1.19126.3")) | .value.value }]'
Still new to jq myself, so any feedback is welcome.
I have the following structure:
{
"Subnets": [
{
"SubnetId": "foo1",
"Id": "bar1",
"Tags": [
{
"Key": "Name",
"Value": "foo"
},
{
"Key": "Status",
"Value": "dev"
}
]
},
{
"SubnetId": "foo2",
"Id": "bar2",
"Tags": [
{
"Key": "Name",
"Value": "foo"
},
{
"Key": "Status",
"Value": "dev"
}
]
}
]
}
I can extract multiple keys at the "top level" like so:
cat subnets.json| jq '.Subnets[] | "\(.Id) \(.SubnetId)"'
Anyone know how I can also display one of the tags by key name, let's say I also want the Status tag displayed on the same line as the Id and SubnetId.
Thx for any help,
Is this what you are looking for?
jq '.Subnets[] | "\(.Id) \(.SubnetId) \(.Tags | from_entries | .Status)"' subnets.json
This question already has an answer here:
How do I extract a key with jq based on its child values
(1 answer)
Closed 2 years ago.
Dear members of support,
I need to get a key by value. Specifically, I need to get the key 2 search by value AWS2. I am trying to use this example [1] without success. Could you help, please?
{
"1": ["AWS1"],
"2": ["AWS2"],
"3": ["AWS3"]
}
The entire json is this one
{
"ARN": "xxxxx",
"Name": "xxxx",
"Description": "xxxx",
"KmsKeyId": "xxxx",
"RotationEnabled": true,
"RotationLambdaARN": "arggg",
"RotationRules": {
"AutomaticallyAfterDays": 30
},
"LastRotatedDate": "2020-05-27T12:05:56.061000-03:00",
"LastChangedDate": "2020-05-27T13:05:34.807000-03:00",
"LastAccessedDate": "2020-05-26T21:00:00-03:00",
"Tags": [
{
"Key": "aws:cloudformation:stack-name",
"Value": "medusa-monitoring-alerts-role"
},
{
"Key": "aws:cloudformation:logical-id",
"Value": "xxx"
},
{
"Key": "Team",
"Value": "xxx xxx"
},
{
"Key": "aws:cloudformation:stack-id",
"Value": "xxx"
}
],
"VersionIdsToStages": {
"1": [
"AWS1"
],
"2": [
"AWS2"
],
"3": [
"AWS3"
]
}
}
I am trying
.VersionIdsToStages|map_values(select(contains(["AWS2"]))) | keys[0]
without sucess.
[1] https://github.com/stedolan/jq/issues/60
You could use the following :
map_values(select(contains(["AWS2"]))) | keys
map_values lets you iterate on the key/value pairs of an object and transform them. Here we use it to remove the key/value pairs with values that do not contain "AWS2". Then we simply use keys to retrieve the keys of those pairs from the transformed object.
If you can assume that only a single value will ever match, you can add a [0] to the end to retrieve the key only rather than an array containing it.
You can try it here.
I have two JSON files as follows.
One contains a mapping of project to owners.
owners.json
{
"Project1": "owner1",
"Project2": "owner2"
}
The second contains a list of projects with extra information:
projects.json
[
{
"name": "Project1",
"base": "A"
},
{
"name": "Project2",
"base": "B"
}
]
I'd like to use JQ to merge the two files to look like the following:
output.json
[
{
"name": "Project1",
"owner": "owner1",
"base": "A"
},
{
"name": "Project2",
"owner": "owner2",
"base": "B"
}
]
My first thought was to try something like this (assuming projects.json is fed on stdin):
jq --slurpFile owners owners.json '.name as $n | [.[] | {name, base, owner: $owners[0].$n}]'
This gives a syntax error relating to the $n in $owners[0].$n. What's the right way to do this in JQ?
Thanks!
You need to wrap variable references in square brackets for indexing objects with them. Even though you corrected that your script wouldn't work as arrays can't be indexed with strings (.name as $n part).
And don't bother with slurpfile, there are simpler ways.
$ jq 'input as $owners | map(.owner = $owners[.name])' projects.json owners.json
[
{
"name": "Project1",
"base": "A",
"owner": "owner1"
},
{
"name": "Project2",
"base": "B",
"owner": "owner2"
}
]
For an input below:
[{
"commit": {
"author": {
"name": "Stephen Dolan",
"email": "mu#netsoc.tcd.ie",
"date": "2013-06-22T16:30:59Z"
},
"committer": {
"name": "Stephen Dolan",
"email": "mu#netsoc.tcd.ie",
"date": "2013-06-22T16:30:59Z"
},
"message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161"
"url":"https://api.github.com/repos/stedolan/jq/commits/d25341478381063d1c76e81b3a52e0592a7c997f"
},
{
...
}
}]
How can JQ generate a delimited string from different objects as shown below?
"Stephen Dolan", "https://api.github.com/repos/stedolan/jq/commits/d25341478381063d1c76e81b3a52e0592a7c997f", "2013-06-22T16:30:59Z"
Collect the fields you want in an array and use #csv to convert to a CSV row. Make sure you get the raw output.
jq -r '.[] | [ .commit.author.name, .commit.url, .commit.author.date ] | #csv' input.json