Getting key from json using jsonpath not working - jsonpath

input json
{
"1212": [
{
"size": "M",
"colour": "RED"
},
{
"size": "L",
"colour": "BLUE"
},
{
"size": "XL",
"colour": "GREEN"
}
]
}
I want here 1212 as output using jsonPath.
I tried jsonpath as $.*~ which works online jsonpath evaluator and give result as [1212].
But when i use same jsonpath in my code it doesn't work. Ex -
JsonPath.read("jsonobject", "$.*~");
I get output as
[
{
"size": "M",
"colour": "RED"
},
{
"size": "L",
"colour": "BLUE"
},
{
"size": "XL",
"colour": "GREEN"
}
]
Dependency for jsonpath in my code
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.6.0</version>
</dependency>

If you are able to change the JSON to make it as an array you can extract the key using keys() function.
JSON
[{
"1212": [{
"size": "M",
"colour": "RED"
},
{
"size": "L",
"colour": "BLUE"
},
{
"size": "XL",
"colour": "GREEN"
}
]
}]
JSONPath
$.[*].keys()

Related

Simplify array of objects with children using JQ

From Wikidata, I get the following json:
# Sparql query
query=$(cat ./myquery.sparql)
response=$(curl -G --data-urlencode query="${query}" https://wikidata.org/sparql?format=json)
echo "${response}" | jq '.results.bindings'
[
{
"language": {
"type": "uri",
"value": "https://lingualibre.org/entity/Q100"
},
"wikidata": {
"type": "literal",
"value": "Q36157"
},
"code": {
"type": "literal",
"value": "lub"
}
},
{
"language": {
"type": "uri",
"value": "https://lingualibre.org/entity/Q101"
},
"wikidata": {
"type": "literal",
"value": "Q36284"
},
"code": {
"type": "literal",
"value": "srr"
}
}
]
I would like to have the keys directly paired with their values, such as :
[
{
"language": "https://lingualibre.org/entity/Q100",
"wikidata": "Q36157",
"iso": "lub"
},
{
"language": "https://lingualibre.org/entity/Q101",
"wikidata": "Q36284",
"iso": "srr"
}
]
I currently have a non-resilient code, which will break whenever the key names change :
jq 'map({"language":.language.value,"wikidata":.wikidata.value,"iso":.code.value})'
How to pair the keys with their values in a resilient way (not naming the keys) ?
I want to "prune" the child objects so to only keep the value.
You could use map_values which works like the outer map but for objects, i.e. it retains the object structure, including the field names:
jq 'map(map_values(.value))'
[
{
"language": "https://lingualibre.org/entity/Q100",
"wikidata": "Q36157",
"code": "lub"
},
{
"language": "https://lingualibre.org/entity/Q101",
"wikidata": "Q36284",
"code": "srr"
}
]
Note that this solution lacks the name conversion from code to iso.

How to use ObjectSizeGreaterThan and ObjectSizeLessThan xml tags for AWS S3 command 'PutBucketLifeCycleConfiguration"?

Trying with a json input like this:
{
"Rules": [
{
"Expiration": {
"Date": "2023-01-01T00:00:00.000Z"
},
"ID": "id1",
"Filter": {
"And": {
"Tags": [
{
"Key": "k1",
"Value": "k1val1"
},
{
"Key": "k1u",
"Value": "k1vadl1"
}
],
"ObjectSizeGreaterThan": 100,
"ObjectSizeLessThan": 1000
}
},
"Status": "Enabled"
}
]
}
However, I am getting an error like this from the aws client:
Parameter validation failed:
Unknown parameter in LifecycleConfiguration.Rules[0].Filter.And: "ObjectSizeGreaterThan", must be one of: Prefix, Tags
Any idea why I get this error and how to correct it?
This seems to be working but only with the latest cli version aws-cli/2.7.30
I am getting the same error on aws-cli/1.19.39
{
"Rules": [
{
"Expiration": {
"Date": "2024-01-01T00:00:00.000Z"
},
"ID": "123",
"Filter": {
"And": {
"Prefix": "sricli",
"Tags": [
{
"Key": "ke",
"Value": "k1"
},
{
"Key": "je",
"Value": "k2"
}
],
"ObjectSizeGreaterThan": 102400,
"ObjectSizeLessThan": 204800
}
},
"Status": "Enabled"
}
]
}
Sri

JSON path help required for a request

I am new at JSON path request and I have a quite complex request to build.
I work with a JSOn strusture having 2 arrays like this example :
{
"WideXml": {
"Guid": "9cf379c5-dc12-4a63-922a-d242efe9a777",
"ApplicationGuid": "24df8af4-58c2-40dd-8ce8-70becb2df96f",
"Action": "Approval",
"Values": {
"Date": {
"TimeStamp": "2021-11-23T04:00:00Z",
"Value": [{
"Guid": "9c64fb06-60f5-4541-a006-3a92ac576e13",
"Value": "6.7169265747070313",
"Unit": "t",
"UserFields": {
"Field": [{
"Value": "131",
"Key": "BWART"
},
{
"Value": "14702-00-BULK",
"Key": "MATNR"
}
]
}
},
{
"Guid": "6c048d70-1521-4fa1-a462-669730d6b1ed",
"Value": "84.824371337890625",
"Unit": "t",
"UserFields": {
"Field": [{
"Value": "261",
"Key": "BWART"
}, {
"Value": "14366-00-WA0R",
"Key": "MATNR"
}]
}
}
]
}
}
}
}
I need to find the "MATNR" code by searching with Key = 'BWART' and Value = '131'.
I can find the Field document by the request
$.WideXml.Values.Date.Value[*].UserFields.Field[?(#.Key=='BWART' && #.Value=='131')]
But I don't manage to build the query to get the MATNR after having this result...
Can someone help?
Regards.
if the key BWART is always at the same index position i.e 0 in this example, you can try the expression
$.WideXml.Values.Date.Value[?(#.UserFields.Field[0].Key== "BWART" && #.UserFields.Field[0].Value == "131")].UserFields.Field[*]

Python 3.6 JSON string to Object conversion

How to convert the following json string to a Python object?
{
"action": "create",
"data": {
"geometry": {
"coordinates": [
-95.12,
16.52,
-52.0
],
"type": "Point"
},
"id": "20180303_0000046",
"properties": {
"auth": "UNM",
"depth": [{
"pacific": {
"value":52.0
}
}],
"evtype": "ke",
"flynn_region": "OAXACA, MEXICO",
"lastupdate": "2018-03-03T10:26:00.0Z",
"lat": 16.52,
"lon": -95.12,
"mag": 4.0,
"magtype": "m",
"source_catalog": "EMSC-RTS",
"source_id": "652127",
"time": "2018-03-03T07:09:05.0Z",
"unid": "20180303_0000046"
},
"type": "Feature"
}
}
I want to read quake.data.properties.depth[0].pacific
I have used the following ref as example, but that code could not read "pacific" attribute.
ref: Creating a Python object from a Json string

How to apply SCAN query using FilterExpression for nested Json attributes in DynamoDB Query in PHP

[{
"forms": [{
"id": "f1",
"title": "Form1",
"update_history": [{
"version": "A",
"updated_at": "2016-12-10 12:12:10"
}, {
"version": "B",
"updated_at": "2017-01-01 05:17:19"
}, {
"version": "C",
"updated_at": "2017-02-07 03:22:39"
}]
},{
"id": "f2",
"title": "Form2",
"update_history": [{
"version": "B",
"updated_at": "2016-12-10 12:12:10"
}, {
"version": "C",
"updated_at": "2017-01-01 05:17:19"
}, {
"version": "D",
"updated_at": "2017-02-07 03:22:39"
}]
},{
"id": "f3",
"title": "Form13",
"update_history": [{
"version": "A",
"updated_at": "2016-12-10 12:12:10"
}, {
"version": "E",
"updated_at": "2017-01-01 05:17:19"
}, {
"version": "F",
"updated_at": "2017-02-07 03:22:39"
}]
}]
}]
I have above data stored in Dynamo-DB table. forms object is parent object. There are 3 list data in update_history nested object. I want all the List data which have version A. Please suggest me query for the same.
The update_history is a List data type contains Map objects on it. This is basically a complex structure on DynamoDB data model.
Assuming, the version A will always be the first occurrence on the List, the below FilterExpression should work. If this assumption is not true, the FilterExpression wouldn't work.
This is the only option available for the above data model. If you can redesign to have only Map or List data type, then we can think about alternate solution.
The below code is on JavaScript:-
You can convert it into PHP code. The AWS SDK API concept is same on all languages.
FilterExpression : 'update_history[0].version = :versionVal',
ExpressionAttributeValues : {
':versionVal' : 'A'
}

Resources