How to disable character escaping by jackson - java

I want to include html in my JSON response.
MyClass obj= new MyCLass();
obj.setHTML("<div style='display:none'>4</div>");
ObjectMapper mapper=new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String jsonResponse=mapper.writeValueAsString(obj);
System.out.println(jsonResponse);
O/P i get
{"html":"<div style=\"display:none\">4</div>"}
Required O/P
{"html":"<div style='display:none'>4</div>"}
Since I want to use the json response directly. Can i disable the escaping of qoutes by object mapper.

You can annotate your getHtml method with
#JsonRawValue

Related

How to compare two JSon Responses when the order of response differ

Below is the response I receive for my two API calls, the response is same but the order of data is out:
{"URI":"CoGroup/2","level":"total","name":null,"code":null,"baseColor":null,"secondaryColor":null,"selected":null,"value":317978,"Byyear":[],"count":10499},
{"URI":"integer/7005","level":"total","name":null,"code":null,"baseColor":null,"secondaryColor":null,"selected":null,"value":26857,"Byyear":[],"count":4542},
{"URI":"intgroup/78","level":"total","name":null,"code":null,"baseColor":null,"secondaryColor":null,"selected":null,"value":105304,"Byyear":[],"count":1653}
]
[
{"URI":"CoGroup/2","level":"total","name":null,"code":null,"baseColor":null,"secondaryColor":null,"selected":null,"value":317978,"Byyear":[],"count":10499},
{"URI":"intgroup/78","level":"total","name":null,"code":null,"baseColor":null,"secondaryColor":null,"selected":null,"value":105304,"Byyear":[],"count":1653},
{"URI":"integer/7005","level":"total","name":null,"code":null,"baseColor":null,"secondaryColor":null,"selected":null,"value":26857,"Byyear":[],"count":4542}
]
I have tried using the Jackson to compare the responses using mapper.readtree, but the results are returning as false.
ObjectMapper mapper1 = new ObjectMapper();
ObjectMapper mapper2 = new ObjectMapper();
try{
assertEquals(mapper1.readTree(respStr1), mapper2.readTree(respStr2));
}
catch(Exception e) {
System.out.println(e);
}
and
ObjectMapper mapper1 = new ObjectMapper();
JsonNode tree1 = mapper1.readTree(respStr1);
JsonNode tree2 = mapper1.readTree(respStr2);
System.out.println(tree1.equals(tree2));
Any suggestions on how to approach for the comparison here....
You can use the library JSONAssert by scyscreamer for this purpose.
It would work like this:
// respStr1 and respStr2 are the two json in string
JSONAssert.assertEquals(respStr1, respStr2, JSONCompareMode.NON_EXTENSIBLE);
NON_EXTENSIBLE mode would allow assertion to pass if the json differs in order only.
Edit:
JSONCompare.compareJSON(respStr1, respStr2, CompareMode.NON_EXTENSIBLE).passed() will return if the comparison passed.

unable to parse json using objectMapper of jackson where json value contains \\

I am de serializing json string to plain java object using jackson's ObjectMapper class. ObjectMapper is throwing an exception
json string I am trying to deserialize is
String input="{\"id\":\"30329\",\"appId\":\"3301\",\"nodeId\":1556537156187,\"data\":\"select id,obt_marks,'\\\\m' as dummy from ltc_test_1\"}";
value for data key contains \ which is causing the problem, is their a way to escape this. i want this value as is in my POJO
It can work by replacing each occurrence of \ with \\ so string will look like
\"data\":\"select id,obt_marks,'\\\\m' as dummy from ltc_test_1\"
Question: How this can be achieved using java,is there any setting in objectMapper or Jackson to tackle this problem ?
Below is pojo which i will get after de-serialization
public class WorkflowProcessInfo {
private Long id;
private Long appId;
private Long nodeId;
private String data;
}
////Code I am using for deserialization
ObjectMapper mapper = new ObjectMapper();
mapper.configure(Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, false);
mapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true); mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY,
true);
mapper.setSerializationInclusion(Include.NON_NULL);
try{
return mapper.readValue(inputJson, WorkflowProcessInfo.class);
}catch(Exception e){
syso(e.getMessage())}
I expecting WorkflowProcessInfo object with values as present in json meaning, data attribute of pojo should look like below
WorkflowProcessInfo.data="select id,obt_marks,'\\m' as dummy from ltc_test_1"
instead i am getting below exception
com.fasterxml.jackson.core.JsonParseException: Unrecognized character
escape 'm' (code 109) at [Source: java.io.StringReader#1ea9f6af;
line: 1, column: 84]

Convert XML to JSON, Issue with jaxb class being used with FasterXML

I need to convert XML to JSON at runtime, using FasterXML and generated beans from JAXB tool xjc using the source XSD.
I am using follwing code
public static void main(String[] args) {
try {
Testing tObj=new Testing();
ObjectMapper tester=tObj.createJaxbObjectMapper();
CustomerOrderType data=tester.readValue(TEST_XML_STRING, CustomerOrderType.class);
//ObjectMapper serializr=new ObjectMapper();
//serializr.writeValue(System.out, data);
} catch (Exception je) {
System.out.println(je.toString());
}
}
public ObjectMapper createJaxbObjectMapper()
{
final ObjectMapper mapper = new ObjectMapper();
final TypeFactory typeFactory = TypeFactory.defaultInstance();
final AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(typeFactory);
// make deserializer use JAXB annotations (only)
mapper.getDeserializationConfig().with(introspector);
// make serializer use JAXB annotations (only)
mapper.getSerializationConfig().with(introspector);
return mapper;
}
But I am getting an error as follows.
com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: (String)"<ns1:orderDetail xmlns:ns1="http://www.colt.net/xml/ns/webservice/manord/v1.0"><ns7:Customer_Order_Status xmlns:ns7="http://www.colt.net/xml/ns/cbe/ord/v1.0">New</ns7:Customer_Order_Status><ns8:Order_Creation_Date xmlns:ns8="http://www.colt.net/xml/ns/cbe/ord/v1.0">2018-03-06T09:03:25</ns8:Order_Creation_Date><ns9:Opportunity_Number xmlns:ns9="http://www.colt.net/xml/ns/cbe/ord/v1.0">8798</ns9:Opportunity_Number><ns10:Order_Source xmlns:ns10="http://www.colt.net/xml/ns/cbe/ord/v1.0">eOrder Lite<"[truncated 9232 chars]; line: 1, column: 2]
It looks that you are using JSON-java lib, which is extremely lightweight and does not provide a possibility to skip namespaces from parsed XML tag names.
But this can be achieved using a more advanced XML/JSON processing lib, for example, FasterXML/Jackson.
Update
To convert String xml to Json:
1) Use com.fasterxml.jackson.dataformat:jackson-dataformat-xml lib dependency.
2) Conversion sequence is similar to:
XmlMapper xmlMapper = new XmlMapper();
MyObject myObj = xmlMapper.readValue(xml, MyObject.class);
ObjectMapper jsonMapper = new ObjectMapper();
String json = jsonMapper.writeValueAsString(myObj);
System.out.println(json);
An off-the-shelf XML-to-JSON converter is never going to give you optimal output for your particular application. You generally need to apply either some preprocessing (an XML transformation using XSLT) or some post-processing of the JSON.
In this case I would suggest doing an XSLT transformation to drop the namespaces.

Convert nested JSON string to list of java object

I have this json String
{"data":"[Level [key=LevelKey [keyEnd=0], Description=abc], Level [key=levelKey [keyEnd=1], Description=xyz]", "id":"123"}
And corresponding java classes are
public class Level {
public LevelKey key;
public String id;
}
public class LevelKey{
public String keyEnd;
}
I want to convert this data json string to list of Level object using Jackson
ObjectMapper mapper = new ObjectMapper();
List<Level> arr = mapper.readValue(data, new TypeReference<List<Level>>(){});
But I am getting below error
com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'Level': was expecting ('true', 'false' or 'null')
Is there any other method to parse it?
The below does not look like a proper JSON for the purpose (except for a standard fixed string)
"[Level [key=LevelKey [keyEnd=0], Description=abc], Level [key=levelKey [keyEnd=1], Description=xyz]"
You could correct the data part of your JSON to something like below (Closest to your JSON in question) :
[\"Level [key=LevelKey [keyEnd=0], Description=abc]\",\" Level [key=levelKey [keyEnd=1], Description=xyz]\"]
Is there any other method to parse it?
You could use a direct class reference of ArrayList instead of having to instantiate TypeReference like below to parse the above (corrected) json string :
List<Level> arr = mapper.readValue(data, (new ArrayList<Level>()).getClass());
This was an interesting one I must say. Take a look at code snippet I think I got it correct :
String data ="{\"data\":\"[Level [key=LevelKey [keyEnd=0], Description=abc], Level [key=levelKey [keyEnd=1], Description=xyz]\", \"id\":\"123\"}";
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
List<Level> arr = mapper.readValue(data, (new ArrayList<Level>()).getClass());
System.out.println(arr);
I got the following output :
[{data=[Level [key=LevelKey [keyEnd=0], Description=abc], Level [key=levelKey [keyEnd=1], Description=xyz], id=123}]
Also if you encountered any JsonParseException which according to documentation means :
Exception type for parsing problems, used when non-well-formed content
(content that does not conform to JSON syntax as per specification) is
encountered.
So while hacking the JSON you can update the ObjectMapper object like this :
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
Also as mentioned by Exception_al using a direct class reference of ArrayList instead of having to instantiate TypeReference like below to parse the above (corrected) json string.
List<Level> arr = mapper.readValue(data, (new ArrayList<Level>()).getClass());
Hope this helped.
The String is not the JSON representation of what you expect you get deserialized into JAVA.
This is a JSON String:
"{"data":[{"key":{"keyEnd":0},"Description":"abc"},{"key":{"keyEnd":1},"Description":"abc"}],"id":"123"}"
So, there is either a problem with the String or you need to do the parsing yourself.

Add a property to a json string with jackson json

I am storing a json string into a text field in mysql.
After the insertion, i want to update my json string and add the mysql line id into it with jackson json.
I have a java String which is in Json format
{
"thing":"val"
}
I'm looking to add another K/V without writing lines of codes.
to finally have this :
{
"thing":"val"
"mysqlId":10
}
I can convert my String to a JsonNode :
ObjectMapper mapper = new ObjectMapper();
JsonNode json = mapper.readTree( jsonStr);
Looking to do something like this
json.put("mysqlId",10);
json.toString();
then update in my text field with new json string in mysql
I can't make it.
I don't want use many class is there a simple way to do so with jackson?
Try casting your JsonNode to an com.fasterxml.jackson.databind.node.ObjectNode and then calling put set (or replace) on it.

Categories