I am trying to return the value "Welcome!" where the object contains "name" key that equals to "Subject". The response body data is as follows:
"payload": {
"mimeType": "multipart/alternative",
"headers": [
{
"name": "Mime-Version",
"value": "1.0"
},
{
"name": "Subject",
"value": "Welcome!"
},
{
"name": "To",
"value": "Jane Doe <xyz#xyz.com>"
},
{
"name": "Message-ID",
"value": "<123456.abc.com>"
}
The following snippet will return the first encountered "value": "1.0", but i need to get the value where name = "Subject". How can i filter the results and check for a matching string?
Response response = SerenityRest.rest()
.contentType("application/json")
.get("URL")
response.then().statusCode(200);
String subject = response.jsonPath().getString("payload.headers.value");
The value "Welcome" is not static but "name": "Subject" will never change.
I'm not sure to understand your question, if you are looking for a jsonpath filter to get the value associate with the field "name": "Subject", this should should normally works:
"payload.headers[?(#.name=='Subject')].value"
But I take a look at json-path rest-assured which is the library used by Serenity rest to perform json-path operation, and that library does not use standard json-path synthax but a synthax based on groovy lambda to perform advanced search. So this should work:
"headers.find{ it.name == 'Subject' }.value"
I test with a complete example:
JsonPath.from("{\"payload\": {\n" +
" \"mimeType\": \"multipart/alternative\",\n" +
" \"headers\": [\n" +
" {\n" +
" \"name\": \"Mime-Version\",\n" +
" \"value\": \"1.0\"\n" +
" },\n" +
" {\n" +
" \"name\": \"Subject\",\n" +
" \"value\": \"Welcome!\"\n" +
" },\n" +
" {\n" +
" \"name\": \"To\",\n" +
" \"value\": \"Jane Doe <xyz#xyz.com>\"\n" +
" },\n" +
" {\n" +
" \"name\": \"Message-ID\",\n" +
" \"value\": \"<123456.abc.com>\"\n" +
" }\n" +
" ]\n" +
"}}")
.getString("headers.find{ it.name == 'Subject' }.value");
// returns "Welcome!"
This blog helps me to understand synthax change:
What's new in REST Assured 1.8?
Related
I have a JSON String and convert it to JSONObject.
I want to get specific data from the JSONObject, and every time the JSONObject changes its structure, sometimes it's in an array inside the JSON and sometimes not.
example:
the first time the JSON arrives like this
{
"id": "1",
"Name": "Jack",
"Value": {
"data": [
{"time": "2023", "age": "22"}
]
}}
the second time
{
"age": "22",
"time": "2023",
"Value": {
"data": [
{"Name": "Jack", "id": "1" }
]
}}
if I want to get the name in the first JSON
jsonObject.getString("Name")
and for the second one, I would use
jsonObject.getJSONArray("data").getJSONObject(0).getString("Name")
is there a way I can get the value dynamically regardless of where the keys are?
If your API come from an another team or an external provider, the first thing I would suggest to you, is to clearly define a contract. Otherwise, you can use the isNull(String key) method of JSONObject to check if the key exists or not.
An example here:
JSONObject jsonObject = new JSONObject(YOUR_JSON_STRING);
String nameValue;
if(jsonObject.isNull("Name")) {
nameValue = jsonObject.getJSONObject("Value")
.getJSONArray("data")
.getJSONObject(0)
.getString("Name");
} else {
nameValue = jsonObject.getString("Name");
}
System.out.println(nameValue);
If the JSON strings are always in a similar fashion then you can try a little parser method as provided below. It returns a Key/Value (String/Object) Map:
public static java.util.Map<String, Object> mapJsonObject(String jsonString) {
String json = jsonString
.replaceAll("(?i)[\\[\\]\\{\\}\"]|\"?value\"?:|\"?data\"?:|\n?", "")
.replaceAll("\\s+", " ");
String[] keyValueParts = json.split("\\s*,\\s*");
java.util.Map<String, Object> map = new java.util.HashMap<>();
for (String str : keyValueParts) {
String[] pts = str.split("\\s*:\\s*");
map.put(pts[0].trim(), pts[1]);
}
return map;
}
To use:
String jsonString = "{\n"
+ " \"id\": \"1\",\n"
+ " \"Name\": \"Jack\",\n"
+ " \"Value\": {\n"
+ " \"data\": [\n"
+ " {\"time\": \"2023\", \"age\": \"22\"}\n"
+ " ]\n"
+ "}}";
java.util.Map<String, Object> map = mapJsonObject(jsonString);
System.out.println(map);
The console window will display:
{id=1, time=2023, age=22 , Name=Jack}
You may consider library Josson.
https://github.com/octomix/josson
Deserialization
Josson josson1 = Josson.fromJsonString(
"{" +
" \"id\": \"1\"," +
" \"Name\": \"Jack\"," +
" \"Value\": {" +
" \"data\": [" +
" {\"time\": \"2023\", \"age\": \"22\"}" +
" ]" +
" }" +
"}");
Josson josson2 = Josson.fromJsonString(
"{ " +
" \"age\": \"22\"," +
" \"time\": \"2023\"," +
" \"Value\": {" +
" \"data\": [" +
" {\"Name\": \"Jack\", \"id\": \"1\" }" +
" ]" +
" }" +
"}");
Query
*() is a multi-level wildcard search. It returns the first resolvable element.
System.out.println(josson1.getString("coalesce(Name, *().Name)"));
// Output: Jack
System.out.println(josson2.getString("coalesce(Name, *().Name)"));
// Output: ["Jack"]
// It is because "Name" is inside array "data".
System.out.println(josson1.getString("coalesce(Name, *().Name).first()"));
// Output: Jack
System.out.println(josson2.getString("coalesce(Name, *().Name).first()"));
// Output: Jack
// Added function first() to extract the value.
I am using RestAssured library to automate API responses in Java language and here is the API body I use:
{
"meta": {
"language": "en",
"marketCountry": "IN",
"sourceUrl": "https://cj-gaq-dev.logistics.dhl/regular-shipment.html?en-AU"
},
"contactInformation": {
"company": "test",
"firstName": "test",
"lastName": "test",
"address": "test",
"zip": "test",
"city": "test",
"country": "IN",
"countryDisplay": "India",
"email": "test#test.com",
"phoneNumber": "2324243243",
"comments": "test"
},
"shipmentScale": {
"domestic": false,
"regional": false,
"global": true
},
"shipmentProduct": {
"FREIGHT": {
"numberOfShipments": "50",
"frequency": "WEEKLY",
"checked": true
}
}
instead of using the whole api body I wanna use queryParameters.
Is there a way doing that?
This is what I have been using so far and keep getting 422 status code error:
String result = given().header("Content-Type","application/json" )
.header("Accept","application/json").log().all().queryParam("marketCountry", "IN").queryParam("shipmentScale.domestic", "false")
.queryParam("shipmentScale.regional", "false").queryParam("shipmentScale.global", "true")
.queryParam("shipmentProduct.FREIGHT.checked", "true")
.queryParam("shipmentProduct.FREIGHT.numberOfShipments", "50")
.queryParam("shipmentProduct.FREIGHT.frequency", "WEEKLY")
.queryParam("contactInformation.company", "test")
.queryParam("contactInformation.firstName", "test")
.queryParam("contactInformation.lastName", "test")
.queryParam("contactInformation.address", "test")
.queryParam("contactInformation.zip", "test")
.queryParam("contactInformation.city", "test")
.queryParam("contactInformation.email", "test#test.com")
.queryParam("contactInformation.phoneNumber", "213456")
.queryParam("contactInformation.comments", "test")
.queryParam("contactInformation.country", "IN")
.queryParam("contactInformation.comments", "test")
.when().post().then().assertThat().statusCode(200).extract().response().asString();
A simple approach is to using RestAssured library to automate API responses in Java language. See the java class below:
public class Basics {
public static void main(String[] args) {
RestAssured.baseURI = "https://12.23.454.55";
given().log().all().queryParam("key", "mykey123").header("Content-Type","application/json")
.body("{\r\n" +
"\r\n" +
"\r\n" +
"\r\n" +
"\"meta\": {\r\n" +
" \"language\": \"en\",\r\n" +
" \"marketCountry\": \"IN\",\r\n" +
" \"sourceUrl\": \"https://cj-gaq-dev.logistics.dhl/regular-shipment.html?en-AU\"\r\n" +
"},\r\n" +
"\"contactInformation\": {\r\n" +
" \"company\": \"test\",\r\n" +
" \"firstName\": \"test\",\r\n" +
" \"lastName\": \"test\",\r\n" +
" \"address\": \"test\",\r\n" +
" \"zip\": \"test\",\r\n" +
" \"city\": \"test\",\r\n" +
" \"country\": \"IN\",\r\n" +
" \"countryDisplay\": \"India\",\r\n" +
" \"email\": \"test#test.com\",\r\n" +
" \"phoneNumber\": \"2324243243\",\r\n" +
" \"comments\": \"test\"\r\n" +
"},\r\n" +
"\"shipmentScale\": {\r\n" +
" \"domestic\": false,\r\n" +
" \"regional\": false,\r\n" +
" \"global\": true\r\n" +
"},\r\n" +
"\"shipmentProduct\": {\r\n" +
" \"FREIGHT\": {\r\n" +
" \"numberOfShipments\": \"50\",\r\n" +
" \"frequency\": \"WEEKLY\",\r\n" +
" \"checked\": true\r\n" +
" }\r\n" +
"\r\n" +
"\r\n" +
"\r\n" +
"}").when().post("api/add/json")
.then().log().all().assertThat().statusCode(200);
}
}
If you like you can do more validation of the response or you can use the JsonPath class to parse your json responses in a bid to do further validation.
However, another approach is to keep your json request in a separate class' method and returning the object in your request body. Or better still you can explore the possibilities of a POJO java class.
This is the json which I am getting
{
"id": 21,
"code": "import scala.collection.JavaConversions._;import java.io.File;def getFileTree(f: File): Stream[File] =f #:: (if (f.isDirectory){ f.listFiles().toStream} else{ Stream.empty});getFileTree(new File(\"/home/datagaps/Downloads/\")).filter(_.getName.endsWith(\".json\")).foreach(println);",
"state": "available",
"output": {
"status": "ok",
"execution_count": 21,
"data": {
"text/plain": "/home/datagaps/Downloads/santanu.json\nimport scala.collection.JavaConversions._\nimport java.io.File\ngetFileTree: (f: java.io.File)Stream[java.io.File]\n"
}
},
"progress": 1
}
This is the code which I have written to access the String.
String output=getGETRequestResponse(uri).getJSONObject("output").getJSONObject("data").getString("text/plain");
org.json.JSONException: JSONObject["output"] is not a JSONObject.
at org.json.JSONObject.getJSONObject(JSONObject.java:736)
at com.datagaps.livyservice.service.LivyServerServiceImpl.getFilePaths(LivyServerServiceImpl.java:229)
at com.datagaps.LivyProject.LivyServiceApplication.main(LivyServiceApplication.java:53)
while running the code i am getting the following exception.
You can try something like this:
String output=getGETRequestResponse(uri).getJSONObject("output").getJSONObject("data").toString();
ObjectMapper mapper = new ObjectMapper();
Map<String, String> map = mapper.readValue(output, Map.class);
System.out.ptintln(map.get("text/plain"));
Let me know if what you get.
You can do this also:
String data = "{\n" +
" \"id\": 21,\n" +
" \"code\": \"import scala.collection.JavaConversions._;import java.io.File;def getFileTree(f: File): Stream[File] =f #:: (if (f.isDirectory){ f.listFiles().toStream} else{ Stream.empty});getFileTree(new File(\\\"/home/datagaps/Downloads/\\\")).filter(_.getName.endsWith(\\\".json\\\")).foreach(println);\",\n" +
" \"state\": \"available\",\n" +
" \"output\": {\n" +
" \"status\": \"ok\",\n" +
" \"execution_count\": 21,\n" +
" \"data\": {\n" +
" \"text/plain\": \"/home/datagaps/Downloads/santanu.json\\nimport scala.collection.JavaConversions._\\nimport java.io.File\\ngetFileTree: (f: java.io.File)Stream[java.io.File]\\n\"\n" +
" }\n" +
" },\n" +
" \"progress\": 1\n" +
"}";
//Here data is response which tou get from getGETRequestResponse(uri) so we can replace it like : String data = getGETRequestResponse(uri)
JSONObject jsonRootObject = new JSONObject(data);
final String value = jsonRootObject.getJSONObject("output").getJSONObject("data").getString("text/plain");
System.out.println("text/plain value: " + value);
I have been using JsonPath. However after an issue yesterday where I discovered that the default JsonSmartJsonProvider didn't report an error with an invalid document at parse time, I modified my setup to use Jackson as below
public JsonPathExtractor(String document) throws DocumentFormatException
{
try
{
Configuration.setDefaults(new Configuration.Defaults()
{
private final JsonProvider jsonProvider = new JacksonJsonProvider();
private final MappingProvider mappingProvider = new JacksonMappingProvider();
#Override
public JsonProvider jsonProvider()
{
return jsonProvider;
}
#Override
public MappingProvider mappingProvider()
{
return mappingProvider;
}
#Override
public Set<Option> options()
{
return EnumSet.noneOf(Option.class);
}
});
// Get an object representation of the JSON to allow values to be extracted
this.document = Configuration.defaultConfiguration().jsonProvider().parse(document);
}
catch(Exception e)
{
throw new DocumentFormatException("Invalid JSON document", e);
}
}
However I see a difference in behaviour, in that if I get a path which has a few fields, they are not quoted, whereas they were when using JsonSmartJsonProvider.
Example JSON
{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 25,
"height_cm": 167.6,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
}
],
"children": [],
"spouse": null
}
With the call:
Object obj = JsonPath.read(document, "$.phoneNumbers");
When using JacksonMappingProvider I get
[{type=home, number=212 555-1234}, {type=office, number=646 555-4567}]
When using JsonSmartJsonProvider I get:
[{"type":"home","number":"212 555-1234"},{"type":"office","number":"646 555-4567"}]
If I want Jackson to behave the same way, is there something else that I can configure?
There's a difference between the way in which Jackson has handled the values and the way in which they are printed out.
When using JsonSmartJsonProvider this line ...
JsonPath.read(parse, "$.phoneNumbers");
... returns a JSONArray and the toString() method - which is called when you 'print' the JSONArray instance is smart enough to know it is dealing with JSON so it prints that state as a JSON string. For example:
[{"type":"home","number":"212 555-1234"},{"type":"office","number":"646 555-4567"}]
But when you use a JacksonJsonProvider then this line ...
JsonPath.read(parse, "$.phoneNumbers");
... returns a List of LinkedHashMap and the toString() implementation invoked when you 'print' that instance is not JSON aware so it prints this:
[{type=home, number=212 555-1234}, {type=office, number=646 555-4567}]
If you want to print JSON when using the JacksonJsonProvider then you have to print it using something which is JSON aware. Here's an example:
String payload = "{\n" +
" \"firstName\": \"John\",\n" +
" \"lastName\": \"Smith\",\n" +
" \"isAlive\": true,\n" +
" \"age\": 25,\n" +
" \"height_cm\": 167.6,\n" +
" \"address\": {\n" +
" \"streetAddress\": \"21 2nd Street\",\n" +
" \"city\": \"New York\",\n" +
" \"state\": \"NY\",\n" +
" \"postalCode\": \"10021-3100\"\n" +
" },\n" +
" \"phoneNumbers\": [\n" +
" {\n" +
" \"type\": \"home\",\n" +
" \"number\": \"212 555-1234\"\n" +
" },\n" +
" {\n" +
" \"type\": \"office\",\n" +
" \"number\": \"646 555-4567\"\n" +
" }\n" +
" ],\n" +
" \"children\": [],\n" +
" \"spouse\": null\n" +
"}";
// this is a simpler way of declaring and using the JacksonJsonProvider
ObjectMapper objectMapper = new ObjectMapper();
Configuration conf = Configuration.builder()
.jsonProvider(new JacksonJsonProvider(objectMapper))
.build();
Object obj = JsonPath.using(conf).parse(payload).read("$.phoneNumbers");
// prints out:
// [{type=home, number=212 555-1234}, {type=office, number=646 555-4567}]
System.out.println(obj);
// prints out:
// [{"type":"home","number":"212 555-1234"},{"type":"office","number":"646 555-4567"}]
System.out.println(objectMapper.writer().writeValueAsString(obj));
I have a JSON file and need to get the parameter ' fulltext ' , but I'm new to JSON and do not know how to retrieve it in Java . Could someone explain to me how caught this value fulltext ?
Here a piece of the file in JSON.
{
"head": {
"vars": [ "author" , "title" , "paper" , "fulltext" ]
} ,
"results": {
"bindings": [
{
"author": { "type": "uri" , "value": "http://data.linkededucation.org/resource/lak/person/richard-scheines" } ,
"title": { "type": "literal" , "value": "Discovering Prerequisite Relationships among Knowledge Components" } ,
"paper": { "type": "uri" , "value": "http://data.linkededucation.org/resource/lak/conference/edm2014/paper/492" } ,
"fulltext": { "type": "literal" , "value": "GET TEXT" }
} ,
Json library download from here jar dowonload form here
Add this code in JSonParsing.java
import org.json.*;
public class JSonParsing {
public static void main(String[] args){
String source = "{\n" +
" \"head\": {\n" +
" \"vars\": [ \"author\" , \"title\" , \"paper\" , \"fulltext\" ]\n" +
" } ,\n" +
" \"results\": {\n" +
" \"bindings\": [\n" +
" {\n" +
" \"author\": { \"type\": \"uri\" , \"value\": \"http://data.linkededucation.org/resource/lak/person/richard-scheines\" } ,\n" +
" \"title\": { \"type\": \"literal\" , \"value\": \"Discovering Prerequisite Relationships among Knowledge Components\" } ,\n" +
" \"paper\": { \"type\": \"uri\" , \"value\": \"http://data.linkededucation.org/resource/lak/conference/edm2014/paper/492\" } ,\n" +
" \"fulltext\": { \"type\": \"literal\" , \"value\": \"GET TEXT\" }\n" +
" }\n" +
" ]\n" +
" }\n" +
"}\n" +
"";
JSONObject main = new JSONObject(source);
JSONObject results = main.getJSONObject("results");
JSONArray bindings = results.getJSONArray("bindings");
JSONObject firstObject = bindings.getJSONObject(0);
JSONObject fulltextOfFirstObject = firstObject.getJSONObject("fulltext");
String type = fulltextOfFirstObject.getString("type");
String value = fulltextOfFirstObject.getString("value");
System.out.println("Type :"+ type+"\nValue :"+value);
}
}
NOTE: In JSON {} represents jsonObject and [] represents jsonArray.
You can use org.json/Jackson to convert this string to JSONObject.
If it is a JSONObject called val;
then val.get("results").get("bindings").get(0).get("fulltext")
will give you the full text of first element of bindings.
There are many good JSON parsing libraries for Java. Try out Org.JSON (Maven) or Jackson Library (Maven) or my personal favorite Google's GSON Library (Maven) that can convert Java Objects into JSON and back.
I recommend you using https://github.com/alibaba/fastjson , It's easy to use.