Get Key from value using jq [duplicate] - jq

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.

Related

How to get value pairs of the objects from JSON using jq

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'

How to convert json array in to the columns table in kusto

I have one parquet file ,trying to get the data in to the table. In one column it have json with multiple values. Can someone help me how to do in kusto?
Pasting the json's schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"path": {
"type": "string"
},
"partitionValues": {
"type": "object",
"properties": {
"deviceId": {
"type": "string"
},
"date": {
"type": "string"
}
},
"required": [
"deviceId",
"date"
]
},
"size": {
"type": "integer"
},
"modificationTime": {
"type": "integer"
},
"dataChange": {
"type": "boolean"
},
"stats": {
"type": "string"
}
},
"required": [
"path",
"partitionValues",
"size",
"modificationTime",
"dataChange",
"stats"
]
}
If I understood correctly, your PQ file contains a column with a JSON of the specified schema. If you want to ingest it as-is, ingest it into Kusto column with type "dynamic" and query later. If you'd like to ingest just part of this JSON data (like some inner fields), use ingestion mapping and provide appropriate JSON path.
Another option is to ingest as-is into a source table with a Retention policy with SoftDeletePeriod of zero. Define an Update Policy with a KQL query and the transformed data will be pushed into a target table.

Using jq to get arrays where a key within the array is equal to a specific value?

I have been practicing with jq play to try to get all the arrays in a list where website is == "google" and create another json list from that.
https://jqplay.org/s/DKNC2mhOLq
jq: error (at :18): Cannot index array with string "website"
exit status 5
{
"items": [
{
"name":"name1",
"id":"1",
"website":"google"
},
{
"name":"name1",
"id":"1",
"website":"google"
},
{
"name":"name1",
"id":"2",
"website":"jingle"
}
]
Desired output:
[
{
"name":"name1",
"id":"1",
"website":"google"
},
{
"name":"name1",
"id":"1",
"website":"google"
}
]
how can I loop through arrays in a list and look for specific values for specific keys? Thanks for any help or ideas you can provide. I am a begginer with JSON and jq.
Enclose the select with a map, as you want to apply the filter to each array item individually while retaining the surrounding array structure.
jq '.items | map(select(.website == "google"))'
[
{
"name": "name1",
"id": "1",
"website": "google"
},
{
"name": "name1",
"id": "1",
"website": "google"
}
]
Demo

jq - extract multiple fields from a list, with a nested list of key/value pairs

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

How to form inner SubQuery in Gremlin Server (Titan 1.0)?

I'm using Following Query :
g.V(741440).outE('Notification').order().by('PostedDateLong', decr).range(0,1).as('notificationInfo').match(
__.as('notificationInfo').inV().as('postInfo'),
).select('notificationInfo','postInfo')
it is giving following result :
{
"requestId": "9846447c-4217-4103-ac2e-de3536a3c62a",
"status": {
"message": "",
"code": ​200,
"attributes": { }
},
"result": {
"data": [
{
"notificationInfo": {
"id": "c0zs-fw3k-347p-g2g0",
"label": "Notification",
"type": "edge",
"inVLabel": "Comment",
"outVLabel": "User",
"inV": ​749664,
"outV": ​741440,
"properties": {
"ParentPostId": "823488",
"PostedDate": "2016-05-26T02:35:52.3889982Z",
"PostedDateLong": ​635998269523889982,
"Type": "CommentedOnPostNotification",
"NotificationInitiatedByVertexId": "1540312"
}
},
"postInfo": {
"id": ​749664,
"label": "Comment",
"type": "vertex",
"properties": {
"PostImage": [
{
"id": "amto-g2g0-2wat",
"value": ""
}
],
"PostedByUser": [
{
"id": "am18-g2g0-2txh",
"value": "orbitpage#gmail.com"
}
],
"PostedTime": [
{
"id": "amfg-g2g0-2upx",
"value": "2016-05-26T02:35:39.1489483Z"
}
],
"PostMessage": [
{
"id": "aln0-g2g0-2t51",
"value": "hi"
}
]
}
}
}
],
"meta": { }
}
}
I want to get information of Vertex "NotificationInitiatedByVertexId" (Edge Property ) in the response as well.
For that i tried following query :
g.V(741440).outE('Notification').order().by('PostedDateLong', decr).range(0,2).as('notificationInfo').match(
__.as('notificationInfo').inV().as('postInfo'),
g.V(1540312).next().as('notificationByUser')
).select('notificationInfo','postInfo','notificationByUser')
Note : I tried directly with vertex Id in subquery as I wasn't aware how to dynamically get value from edge property in query itself.
It is giving error. I tried a lot but am not able to find any solution.
I'm assuming that you are storing a Titan generated identifier in that edge property called NotificationInitiatedByVertexId. If so, please consider the following even though this first part doesn't really answer your question. I don't think you should store a vertex identifier on the edge. Your graph model should explicitly track the relationship of NotificationInitiatedBy with an edge and by storing the identifier of the vertex on the edge itself you are bypassing that. Also, if you ever have to migrate your data in some way, the ids won't be preserved (Titan will generate new ones) and trying to sort that out will be a mess.
Even if that is not a Titan generated identifier and a logical one you created, I still think I would look to adjust your graph schema and promote that Notification to a vertex. Then your Gremlin traversals would flow more easily.
Now, assuming you don't change that, then I don't see a reason to not just issue two queries in the same request and then combine the results to one data structure. You just need to do a lookup with the vertex id which is going to be pretty fast and inexpensive:
edgeStuff = g.V(741440).outE('Notification').
order().by('PostedDateLong', decr).range(0,1).as('notificationInfo').
... // whatever logic you have
select('notificationInfo','postInfo').next()
vertexStuff = g.V(edgeStuff.get('notificationInfo').value('NotificationInitiatedByVertexId')).next()
[notificationInitiatedBy: vertexStuff, notification: edgeStuff]

Resources