Need a json path expression for below json - json

Need a JSON path expression for below JSON. I wanted to extract "Id" for each specific "name"
For Example: I need to extract "Id" : "3" for "name" : "XYZ" .
I tried a JSON path expression as $..Id which given output as:
[
"1",
"2",
"3"
]
But I needed an Id specific to "name": "XYZ"`
[
{
"primary":{
"name":"ABC"
},
"Id":"1"
},
{
"primary":{
"name":"PQR"
},
"Id":"2"
},
{
"primary":{
"name":"XYZ"
},
"Id":"3"
}
]

Able to resolve this by below expression
$..[?(#.primary.name == 'XYZ')].Id

Related

JsonPath: getting first element in string list

On https://jsonpath.com I have below example, using expression
$.phoneNumbers[?(#.id < 3)].number
on below JSON object.
{
"firstName": "John",
"lastName" : "doe",
"age" : 26,
"phoneNumbers": [
{
"type" : "iPhone",
"number": "0123-4567-1111",
"id": 1
},
{
"type" : "home",
"number": "0123-4567-2222",
"id": 2
},
{
"type" : "home",
"number": "0123-4567-3333",
"id": 3
}
]
}
Result is
[
"0123-4567-1111",
"0123-4567-2222"
]
Question
I only want the first string "0123-4567-1111", but appending [0] to my expression does not work. Expression $.phoneNumbers[?(#.id < 3)].number[0] gives result ["0","0"]. How can I get the first returned string?
Indeed you were very close to it by using this expression -
$.phoneNumbers[?(#.id < 3)].number[0]
In this expression you used id but in the json there is no id key so it resulted in undefined
try the expression like this using index -
$.phoneNumbers[0].number
It will return number from the first object of phoneNumbers list as : ["0123-4567-8888"]
If you want to go by conditional basis use the below expression which will return the number of type iphone -
$.phoneNumbers[?(#.type == 'iPhone')].number
Output -
["0123-4567-8888"]

How to print this specific JSON in a Mat Table?

I have this specific JSON file I'm not allowed to change, which looks like this :
{
"computers" : {
"John" : {
"version" : "1.42.0",
"environment" : "Default",
"platform" : "x64",
"admin" : "true"
},
"Peter" : {
"version" : "1.43.6",
"environment" : "Default",
"platform" : "x64",
"admin" : "true"
},
"Eric" : {
"version" : "1.43.6",
"environment" : "Default",
"platform" : "x64",
"admin" : "false"
}
}
I use the JSON.parse() method to parse the file and I put it into a MatTableDataSource.
Problem is, when I need to display it in my MatTable, I can't really access it the way I want.
I have a column where I want to display all version parameters, so for this I can't say : this.dataSource.computers.????.version
Do you guys see where I'm getting at ? Do you have any idea of what I can do differently in order to solve this ?
Looking forward to reading you.
The Angular mat-table requires the input data to be in an array. First, we use Object.keys() to extract the keys, which will contain the list of names. Then, we can use Object.values() to other values within each key in array format. This is followed by mapping the above array objects with the name property from the list of names.
const data = {
"computers": {
"John": {
"version": "1.42.0",
"environment": "Default",
"platform": "x64",
"admin": "true"
},
"Peter": {
"version": "1.43.6",
"environment": "Default",
"platform": "x64",
"admin": "true"
},
"Eric": {
"version": "1.43.6",
"environment": "Default",
"platform": "x64",
"admin": "false"
}
}
};
const nameList = Object.keys(data.computers);
const dataList = Object.values(data.computers).map((obj, index) => {
obj['name'] = nameList[index];
return obj;
});
console.log(dataList);

Why do consecutive numbers in a JSON tree cause a failed conversion to NSDictionary?

I've been working with Firebase, and in a part of my JSON tree, I had a bunch of consecutive numbers as keys.
Here's an abbreviated portion of that part of the tree:
"matches" : {
"1" : {
"0931-Red" : [
"John Smith"
],
"2022-Blue" : [
"Paul Adams"
]
},
"2" : {
"1489-Red" : [
"Matthew Brown"
],
"1565-Blue" : [
"Ian Fowler"
]
},
"3" : {
"1652-Red" : [
""
],
"2626-Blue" : [
""
]
}
}
When I downloaded it from Firebase and used this:
if let r = ref {
r.child(accessKey).child("matches").observeSingleEvent(of: .value, with: { (snapshot) in
if let groups = snapshot.value as? NSDictionary {
The conversion to NSDictionary failed.
However, when I edited "matches" to look like this:
"matches" : {
"1" : {
"0931-Red" : [
"John Smith"
],
"2022-Blue" : [
"Paul Adams"
]
},
"2" : {
"1489-Red" : [
"Matthew Brown"
],
"1565-Blue" : [
"Ian Fowler"
]
},
"4" : {
"1652-Red" : [
""
],
"2626-Blue" : [
""
]
}
}
It worked.
I was able to convert the data to an NSDictionary.
This also worked by changing the first "1" to "01" and appending a string to the beginning of each number like "match-1", "match-2", etc.
Even though my code works, the error is still bothering me because I don't know why it was wrong in the first place.
Why didn't it work with consecutive numbers as keys in the first place?
Thanks in advance!

How do I give a where clause or condition to get a value in json response using rest asssured

I need to extract the value of id where name == 'abc'. How can I do that?
here is the example of response:
{
"Text": [
{
"id": "123",
"name": "ABC"
},
{
"id": "456",
"name": "XYZ"
},
{
"id": "789",
"name": "DEF"
}
]
}
So I need to extract the value of id where name =='ABC' should return me id value as 123.
I need to use jayway restassured.
Use GPath findAll feature
when().
get("/restapi").
then().
body("text.findAll{ it.name == 'ABC' }.id", hasItem("123"));

Nested filter numerical range

I have the following json object:
{
"Title": "Terminator,
"Purchases": [
{"Country": "US", "Site": "iTunes", "Price": 4.99},
{"Country": "FR", "Site": "Google", "Price": 5.99}
]
}
I want to be able to find an object specifying a Country+Site+PriceRange. For example, the above should return True on Country=US&Price<5.00, but should return False on Country=FR&Price<5.00. How would the index and query look to do this? Here is another answer that this is a follow-up question to: Search within array object.
Simply add a Range query to your Bool query logic tree. This will return documents that match US for country and have the Price field with a numeric value less than 5.
{ "query":
{ "nested" : {
"path" : "Purchases",
"score_mode" : "avg",
"query" : {
"bool" : {
"must" : [
{
"match" : {"Purchases.Country" : "US"}
},
{
"range" : "Purchases.Price":
{
"lte": 5
}
}
]
}
}
}
}
}