could not fetch json object using jsonpath - json

I've a requirement to fetch value from json using jayway jsonpath.
Json structure looks like below
[
{
"type": "a",
"values": [
{
"name": "a",
"value": [1,2,3]
},
{
"name": "b",
"value": [3,4,5]
},
{
"name": "c",
"value": [6,7,8]
}
]
}
]
my requirement is in the values array if the name value is a and value array contains value 1, then I need to fetch value array where name is b.
I have written jsonPath expression like below
$..values[?(#.name == 'a')]
where it is returning only
{
"name": "a",
"value": [1,2,3]
}
could someone help me in writing jsonpath expression please , Thanks in advance.
expected output
[3,4,5]
tried with
$..[?(#.values[?(#.name== 'a' && #.value CONTAINS 1)])]
then it is matching every object present in root array.

With Jayway JSONPATH you might get lucky with below jsonpath
$..[?(#.values[?(#.name=='a')].value[*] contains 1 )].values[?(#.name=='b')].value[*]

Related

How to use jq to produce multiple JSON objects?

How would one transform a JSON object into several derived JSON objects with jq?
Example input:
[
{
"id": 1,
"a": "value-in-a",
"b": "value-in-b"
},
{
"id": 2,
"c": "value-in-c"
}
]
Expected output:
[
{
"id": "1",
"value": "value-in-a"
},
{
"id": "1",
"value": "value-in-b"
},
{
"id": "2",
"value": "value-in-c"
}
]
Here the output is an array with 3 elements. First 2 elements are transformed using the first element in the input array. Third element is produced from second element in the input array.
I assume to achieve there will need to be several steps:
a) Construct 2 objects from single JSON object input. Aassume this can be done using variables. First assign input object into variable, then construct object with value a and then with value b. Not sure how to make JQ return several constructed JSON objects.
b) Conditionals will need to be used to not produce an object if a, or b, or c is missing. This can probably be done using 'alternative' operator or if-then-else
You can iterate over the other keys using del and keys_unsorted:
jq 'map({id, value: (del(.id) | .[keys_unsorted[]])})'
[
{
"id": 1,
"value": "value-in-a"
},
{
"id": 1,
"value": "value-in-b"
},
{
"id": 2,
"value": "value-in-c"
}
]
Demo

Retrieve specific value from a JSON blob in MS SQL Server, using a property value?

In my DB I have a column storing JSON. The JSON looks like this:
{
"views": [
{
"id": "1",
"sections": [
{
"id": "1",
"isToggleActive": false,
"components": [
{
"id": "1",
"values": [
"02/24/2021"
]
},
{
"id": "2",
"values": []
},
{
"id": "3",
"values": [
"5393",
"02/26/2021 - Weekly"
]
},
{
"id": "5",
"values": [
""
]
}
]
}
]
}
]
}
I want to create a migration script that will extract a value from this JSON and store them in its own column.
In the JSON above, in that components array, I want to extract the second value from the component with an ID of "3" (among other things, but this is a good example). So, I want to extract the value "02/26/2021 - Weekly" to store in its own column.
I was looking at the JSON_VALUE docs, but I only see examples for specifing indexes for the json properties. I can't figure out what kind of json path I'd need. Is this even possible to do with JSON_VALUE?
EDIT: To clarify, the views and sections components can have static array indexes, so I can use views[0].sections[0] for them. Currently, this is all I have with my SQL query:
SELECT
*
FROM OPENJSON(#jsonInfo, '$.views[0].sections[0]')
You need to use OPENJSON to break out the inner array, then filter it with a WHERE and finally select the correct value with JSON_VALUE
SELECT
JSON_VALUE(components.value, '$.values[1]')
FROM OPENJSON (#jsonInfo, '$.views[0].sections[0].components') components
WHERE JSON_VALUE(components.value, '$.id') = '3'

Robot Framework how to count item list in JSON

I would like to count item "Start" from JSON APIs on Robot Framework
{
"result": {
"api": "xxx",
"timestamp": "14:41:18",
"series": [
{
"series_code": "test",
"series_name_eng": "test",
"unit_eng": "t",
"series_type": "e",
"frequency": "s",
"last_update_date": "2020",
"observations": [
{
"start": "2020-01",
"value": "999"
},
{
"start": "2020-02",
"value": "888"
},
{
"start": "2020-03",
"value": "777"
},
]
}
I use this not working
${json_string} Get File ./example.json
${json_object} evaluate json.loads('''${json_string}''') json
#${value}= get value from json ${json_object} $.result.series[0].observations
${x_count} Get Length ${json_object["$.result.series[0].observations"]}
Could you please help guide to for how?
The Json provided in the example above is not valid one. That will need to be fixed by closing series array ] then close the results object } and then close the outer object }
Valid json will look like this -
${Getjson}= {"result":{"api":"xxx","timestamp":"14:41:18","series":[{"series_code":"test","series_name_eng":"test","unit_eng":"t","series_type":"e","frequency":"s","last_update_date":"2020","observations":[{"start":"2020-01","value":"999"},{"start":"2020-02","value":"888"},{"start":"2020-03","value":"777"}]}]}}
You were close with this jsonpath $.result.series[0].observations. The correct one is in below example -
${json}= Convert String to JSON ${Getjson}
#{Start}= Get Value From Json ${json} $.result.series[?(#.observations)].observations[?(#.start)].start
${length} Get length ${Start}
log ${length}
Output:-

Replacing the value of JSON based on key value pair with jq

I have a JSON with the following structure
{
"name": "name",
"id": [
"abcdef"
],
"input_dataobjects": [
{
"id": "someid1",
"name": "somename1",
"provider": "someprovider",
"datatype": "somedatatype1"
},
{
"name": "some_name2",
"datatype": "some_datatype2",
"id": "some_id2"
}
]
}
What I am trying to achieve
in input_dataobjects if datatype == somedatatype1 then name = sonemewname1.
I can use the index of the input_dataobjects since my json always has the same structure. But is there any different way to achieve it by parsing through the input_dataobjects and find the index to replace? I am using jq to do JSON operations.
I tried with using the index like .input_dataobjects[0].name="someting" because i know the position of the datatype always.
The simplest and perhaps most efficient solution to the problem as stated is:
.input_dataobjects |=
map( if .datatype == "somedatatype1"
then .name = "sonemewname1"
else . end )

How to get the length of a JSON array using JSONPath?

If I have JSON document like this
[
{
"number" : "650-462-9154",
"type" : "main"
},
{
"number" : "650-462-1252",
"type" : "fax"
}
]
What JSONPath can I use to get the array length (which is 2), without hardcoding any property values?
Using the tool I have, here is some examples they gave, which doesn't help me figure out what value I need.
[
{
"type": "add",
"id": "tt0484562",
"version": 1,
"lang": "en",
"fields": {
"title": "The Seeker: The Dark Is Rising",
"director": "Cunningham, David L.",
"genre": ["Adventure","Drama","Fantasy","Thriller"],
"actor": ["McShane, Ian","Eccleston, Christopher","Conroy, Frances",
"Crewson, Wendy","Ludwig, Alexander","Cosmo, James",
"Warner, Amelia","Hickey, John Benjamin","Piddock, Jim",
"Lockhart, Emma"]
}
},
{
"type": "delete",
"id": "tt0484575",
"link_ref": null,
"version": 2
}
]
$.[0].genre ---> 0
$.[0].fields.genre ---> 1
$.[0].fields.genre[*] ---> 4
$.[*].type ---> 2
$.[1].link_ref ---> 1
You can get the length of the array from the first example by using the .length() function like this :
$.length
That should return something like this:
[
2
]
As for the second example, you can access any array keys using regular dot notation ($.node1.node2.node3) and call the length() function on the node key you wish to get the length of. For example, if you wanted to get the number of values in the actor array you could do something like:
$..actor.length
Which will return something like this:
[
10
]
Tested on https://codebeautify.org/jsonpath-tester and http://jsonpath.com/ .
You can find more functions in the jsonpath github repo.