Get value from json based on condition using JsonPath - rest-assured

I've following json -
{
"issues": [{
"id": 200615,
"custom_fields": [{
"id": 166,
"name": "Severity",
"internal_name": null,
"field_format": "list",
"value": "Major"
}, {
"id": 167,
"name": "Reproducibility",
"internal_name": null,
"field_format": "list",
"value": "Always"
}, {
"id": 168,
"name": "Resolution",
"internal_name": null,
"field_format": "list",
"value": "open"
}]
}]
}
Out of this json I want to get value of issues -> custome_fields -> value where id is 167 and expected value is - Always (167 can be at any position in json hence can't hardcode it)
Could you please how I can achieve the value?
jsonPath.get("issues[0].custom_fields[?(#.id == '167')].value[0]")
results into error -
java.lang.IllegalArgumentException: Invalid JSON expression:
Script1.groovy: 1: expecting EOF, found '[' # line 1, column 50.
Object.issues[0].custom_fields[?(#.id ==

I get the same error as you do and I don't know why.
However, you can achieve the same goal using findAll json path method like this:
issues[0].custom_fields.findAll { it.id == 167 }.value[0]
Prints out: Always

$.issues[0].custom_fields[?(#.id==167)].value

Related

How to Combine the First and Last Name fields into a new field logstash?

Sample Data : {
"_id": "5c53d41e80e3f817d9cec5b0",
"index": 0,
"account_number": 1623,
"balance": 22526.72,
"firstname": "Desiree",
"lastname": "Mays",
"age": 46,
"gender": "female",
"address": "931 Metropolitan Avenue, Faxon, Oregon, 3046",
"employer": "IDEGO",
"email": "desireemays#idego.com",
"city": "Rodanthe",
"state": "Tennessee"
}
Conf File:
input {
file {
type => "json"
path=>'path'
}}filter { json {source => "message"} mutate {add_field => { "field_name" => "%{Firstname}" "%{lastname}"}}}output {
file {
path =>'path'
}}
I Keep on getting the tag _jsonparsefailure
Could you confirm if the input file is a JSON type and if the path is set appropriately? otherwise i don't see an anomaly with add_field syntax here.

Filter empty object in odata

There is an oData service i want to call. We have an model that has an field Parent. The service tells me that the parent is an object with an iv that is an string inside. So we get the following model from the oData service:
{
"total": 0,
"items": [
{
"id": "string",
"data": {
"Title": {
"en": "string",
"nl": "string"
},
"Parent": {
"iv": [
"string"
]
},
}
]
}
Now when we get data back it looks like this:
{
"total": 2,
"items": [
{
"id": "6204c07d-1aef-4bd2-9646-e3ca36c63784",
"data": {
"Title": {
"en": "Test 1",
"nl": "Test 1"
},
"Parent": {
"iv": []
},
},
},
{
"id": "bfd1b084-4166-4fec-9032-08047c8313d2",
"data": {
"Title": {
"en": "Test 2",
"nl": "Test 2"
},
"Parent": {
"iv": [
"6204c07d-1aef-4bd2-9646-e3ca36c63784"
]
},
},
},
}
Now i want to filter on all the items where parent is [] (so no parent selected). I've tried the following filters but all without success
data/Parent/iv eq [] | Returns 0 results
data/Parent/iv eq null | Returns 0 results
data/Parent eq null | Returns 0 results
length(data/Parent/iv) eq 0 | OData operation is not supported
not data/Parent/any() | Any/All may only be used following a collection.
not data/Parent/any() | Any/All may only be used following a collection.
data/Parent/iv/$count eq 0 | Server error
Found that for this case the following works:
empty(data/Parent/iv)

java.lang.IllegalArgumentException: Invalid JSON expression: Script1.groovy: 1: unexpected token: ] # line 1, column 36. weather[*].description

I am new to RestAssured and trying to the following code:
#Test
public void testJsonPath() {
Response response = RestAssured
.given()
.param("id", "2172797")
.param("appid", "439d4b804bc8187953eb36d2a8c26a02")
.when()
.get("https://samples.openweathermap.org/data/2.5/weather");
String value = "weather[*].description";
System.out.println(value);
String data = response.then().contentType(ContentType.JSON).extract().path(value);
System.out.println(data);
}
JSON:
{
"coord": {
"lon": 145.77,
"lat": -16.92
},
"weather": [
{
"id": 802,
"main": "Clouds",
"description": "scattered clouds",
"icon": "03n"
}
],
"base": "stations",
"main": {
"temp": 300.15,
"pressure": 1007,
"humidity": 74,
"temp_min": 300.15,
"temp_max": 300.15
},
"visibility": 10000,
"wind": {
"speed": 3.6,
"deg": 160
},
"clouds": {
"all": 40
},
"dt": 1485790200,
"sys": {
"type": 1,
"id": 8166,
"message": 0.2064,
"country": "AU",
"sunrise": 1485720272,
"sunset": 1485766550
},
"id": 2172797,
"name": "Cairns",
"cod": 200
}
Getting the following error:
java.lang.IllegalArgumentException: Invalid JSON expression:
Script1.groovy: 1: unexpected token: ] # line 1, column 36.
weather[*].description
Why do we get the following error when using * ?. When replacing it String value = "weather[0].description"; it works fine. Can someone please help me with this.
I have checked the postman as well and the API is giving the correct output.
Note: When using creating code (weather[*].description) from http://jsonpath.com/. It gives the same output.
If there is anything I am missing anything please let me know as I am new to it. Any help to this would be great.
It can also be great if someone can give me a brief on this and let me know whom to refer to get the best output from this.
Why do we get the following error when using * ?. When replacing it String value = "weather[0].description"; it works fine. Can someone please help me with this.
Rest Assured uses Groovy's GPath notation and is not to be confused with Jayway's JsonPath syntax
Official Documentation
GPath
JsonPath
Edit :
{"users":[{"firstName":"sijo","lastName":"john","subjectId":1,"id":1},{"firstName":"sonia","lastName":"sahay","subjectId":2,"id":2},{"firstName":"shreya","lastName":"sahay","subjectId":1,"id":3}],"subjects":[{"id":1,"name":"Devops"},{"id":2,"name":"SDET"}]}
For the above JSON you can use the below to fetch all the lastName
JsonPath abc = new JsonPath(res);
String deal = abc.getString("users.lastName");
System.out.println(deal);

using the $filter inside the $expand in the Odata query

I am trying to use the filter in the expanded entity using the Odata query like
https://labstest.science.com/DEV/odata/ROOM_REQUEST?$expand=REQ_COLONYROOM($select=Name;$filter=Name eq 'RB05')
This works partially. It brings all the ROOM_REQUEST with Null for the REQ_COLONYROOM where Name is not equal to RB05
{
"#odata.context": "https://labstest.science.com/DEV/odata/$metadata#ROOM_REQUEST(REQ_COLONYROOM(Name))",
"value": [
{
"Id": 18399308,
"Name": "M1",
"Barcode": "M1",
"REQ_COLONYROOM": null
},
{
"Id": 18403071,
"Name": "M2",
"Barcode": "M2",
"REQ_COLONYROOM": {
"#odata.id": "https://labstest.science.com/odata/COLONY_ROOM('MC19')",
"Name": "RB05"
}
},
{
"Id": 18403109,
"Name": "M3",
"Barcode": "M3",
"REQ_COLONYROOM": null
}]}
Though M1 and M3 has different Colony than RB05 it returns Null with the odata call
Did you try something like this:
https://labstest.science.com/DEV/odata/ROOM_REQUEST?$expand=REQ_COLONYROOM&$select=REQ_COLONYROOM/Name&$filter=REQ_COLONYROOM/Name eq 'RB05'
I think it would be enough not having SELECT:
https://labstest.science.com/DEV/odata/ROOM_REQUEST?$expand=REQ_COLONYROOM&$filter=REQ_COLONYROOM/Name eq 'RB05'

In Watson Discovery, limiting "return"ed fields to aggregation values

For the Discovery REST api, the argument/parameter "return" controls which fields are returned.
So if I pass these arguments to the API
{
"query": named_sector,
"count": "10",
"filter": filter_dates,
"aggregation" : "term(docSentiment.type,count:3)"
}
my_query = discovery.query(my_disc_environment_id, my_disc_collection_id, qopts)
print(json.dumps(my_query, indent=2))
I get the following:
{
"matching_results": 14779,
"aggregations": [
{
"type": "term",
"field": "docSentiment.type",
"count": 3,
"results": [
{
"key": "positive",
"matching_results": 4212
},
{
"key": "negative",
"matching_results": 3259
},
{
"key": "neutral",
"matching_results": 152
}
]
}
],
"results": [
{
"id": "6389715fe7e7f711e0bc09d4f1236639",
"score": 1.3689895,
"yyyymm": "201704",
"url": "https://seekingalpha.com/article/4060446-valuation-dashboard-consumer-discretionary-update",
"enrichedTitle": null,
"host": "seekingalpha.com",
"text": "Valuation Dashboard: Consumer Discretionary - Update\n\nSummary\n\nValuation metrics in Consumer Discretionary.\n\nEvolution since last month.\n\nA list of stocks loo ....
and thousands of more lines. How do I restrict the output to the aggregations section? Is this an issue of me better handling the JSON structure that is returned?
thanks
If you change the count argument to 0, the returned JSON will only contain the aggregations.
Also, if you're using the Discovery web tooling, you can enter 0 for the "Number of results to return (Count)" field.
More details and an example can be found here: https://www.ibm.com/watson/developercloud/doc/discovery/using.html#building-aggregations

Resources