I am writing api tests. I am using rest assured to make the requests the following way:
public void POSTNewRequest(String endpoint, String requestBody){
response = given().auth().none()
.header("Content-Type", "application/json")
.contentType(ContentType.JSON)
.when()
.body(requestBody).log().all()
.post(endpoint);
}
The requestBody I'm passing in the request is constructed by converting custom java objects to a string using ObjectMapper.writeValueAsString(requestBody).
This method has been working great for me, but now I need to make a bunch of requests where there is a certain field missing. eg:
{
"foo": [
{
"description": "dflt desc",
"ref": "abcd",
"FIELDTOREMOVE": 0,
"customArray": {
"number": 22,
"letter": "B"
}
}
],
"moreInfo": {
"email": "test#test.be",
"name": "Jhon Doe"
}
}
Now I would like to remove the field "FIELDTOREMOVE" inside this request just before the post method. I tried to convert the requestBody string to a JsonNode and then removing the field but it doesn't remove the field.
private void removeNullFields(String requestBody) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(requestBody);
System.out.println(jsonNode.get("FIELDTOREMOVE"));
((ObjectNode)jsonNode).remove("FIELDTOREMOVE");
}
And when I try to print the value the field its returning "null" so I'm obviously doing something wrong...
I also tried achieving the same by using the gson library with similar results so I guess there is a misunderstanding on my end but can't figure out where to look to fix my problem.
In short: I'm making api requests using the rest assured library by passing a string as the body but in this string I sometimes have to remove certain fields the check what response I'm getting.
The "foo" in your JSON is an array. You should do:
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(requestBody);
jsonNode.get("foo").forEach(e -> ((ObjectNode) e).remove("FIELDTOREMOVE"));
System.out.println(jsonNode.toPrettyString());
Output:
{
"foo" : [ {
"description" : "dflt desc",
"ref" : "abcd",
"customArray" : {
"number" : 22,
"letter" : "B"
}
} ],
"moreInfo" : {
"email" : "test#test.be",
"name" : "Jhon Doe"
}
}
You can use this library:
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
</dependency>
So that the code would look like:
import org.json.JSONObject;
public class JSONManipulation {
static final String JSON = "{\n" +
" \"foo\": [\n" +
" {\n" +
" \"description\": \"dflt desc\",\n" +
" \"ref\": \"abcd\", \n" +
" \"FIELDTOREMOVE\": 0,\n" +
" \"customArray\": {\n" +
" \"number\": 22,\n" +
" \"letter\": \"B\"\n" +
" }\n" +
" }\n" +
" ],\n" +
" \"moreInfo\": {\n" +
" \"email\": \"test#test.be\",\n" +
" \"name\": \"Jhon Doe\"\n" +
" }\n" +
"}";
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject(JSON);
System.out.println("Value before edit: " + jsonObject.toString());
jsonObject.getJSONArray("foo").getJSONObject(0).remove("FIELDTOREMOVE");
System.out.println("Value after edit: " + jsonObject.toString());
}
}
The output would be:
Value before edit: {"foo":[{"FIELDTOREMOVE":0,"ref":"abcd","description":"dflt desc","customArray":{"number":22,"letter":"B"}}],"moreInfo":{"name":"Jhon Doe","email":"test#test.be"}}
Value after edit: {"foo":[{"ref":"abcd","description":"dflt desc","customArray":{"number":22,"letter":"B"}}],"moreInfo":{"name":"Jhon Doe","email":"test#test.be"}}
So you can manipulate with your JSON content and then post is as a String.
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 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'm trying to parse below JSON and looking for "zip-code" value "526262". I'm new to Java and struggling to get the zip-code value?
This is my JSON:
{
"id": "6fffdfdf-8d04-4f4e-b746-20930671bd9c",
"timestamp": "2017-07-21T03:51:27.329Z",
"lang": "en",
"result": {
"source": "testsrc",
"resolvedQuery": "testquery",
"action": "test",
"actionIncomplete": true,
"parameters": {
"zip-code": "526262"
}
}
}
And this is my Java code:
String test= "{\n" +
"\t\"id\": \"6fffdfdf-8d04-4f4e-b746-20930671bd9c\",\n" +
"\t\"timestamp\": \"2017-07-21T03:51:27.329Z\",\n" +
"\t\"lang\": \"en\",\n" +
"\t\"result\": {\n" +
"\t\t\"source\": \"testsrc\",\n" +
"\t\t\"resolvedQuery\": \"testquery\",\n" +
"\t\t\"action\": \"test\",\n" +
"\t\t\"actionIncomplete\": true,\n" +
"\t\t\"parameters\": {\n" +
"\t\t\t\"zip-code\": \"526262\"\n" +
"\t\t}\n" +
"\t}\n" +
"}";
JSONObject request = new JSONObject(test);
String zipCode = request.getJSONObject("result").get("parameters").toString();
System.out.println("zipCode is : " + zipCode);
But I'm getting below output:
zipCode is : {"zip-code":"526262"}
How to get zip-code value alone?
Can someone help how to get this value in java?
You should use getJSONObject when getting parameters so that you can keep using the JSONObject API to dig deeper.
request.getJSONObject("result").getJSONObject("parameters").getString("zip-code");
request.getJSONObject("result").get("parameters").getString("zip_code")
will solve your problem. JSON objects are built to handle nesting.
My Json looks something like this:
{
"source": "somedatabasename",
"lisofobjects": [{
"data": {
"starttime": "145756767377",
"age": "20",
"name": "xyz"
}
}]
}
As a part of validation of my API I want to pass string in age attribute for example age: "xyz". I have created a POJO objects and parsing json using gson.I want to know how should I setvalueof age at runtime.So my request code looks like this:
protected RequestSpecification abc(classnamefromwherejsonisparsed object)
throws JsonSyntaxException, FileNotFoundException, IOException {
RequestSpecBuilder builder = new RequestSpecBuilder();
builder.setContentType(ContentType.JSON);
builder.setBody(object.toJson(object.getallTestData()));
Thus here with getallTestData I want to change only 1 value for example age here. something like object.setAge("abc")
If I understand you correctly you need to generate different invalid test data from sample json.
In that case this coud work:
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
...
String json =
"{\n" +
"\"source\": \"somedatabasename\",\n" +
"\"lisofobjects\": [{\n" +
" \"data\": {\n" +
" \"starttime\": \"145756767377\",\n" +
" \"age\": \"20\",\n" +
" \"name\": \"xyz\"\n" +
" }\n" +
"}]" +
"}";
JsonParser parser = new JsonParser();
JsonObject rootObject = parser.parse(json).getAsJsonObject();
JsonObject firstData = rootObject.getAsJsonArray("lisofobjects")
.get(0).getAsJsonObject().getAsJsonObject("data");
firstData.remove("age");
firstData.addProperty("age", "abc");
String modifiedJson = new Gson().toJson(rootObject);
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.