I'm retrieving data from a rest service and I'm using JSON to map the JSON response to java POJOs. All works fine, except the service will return a different JSON result for invalid calls, which is not mappable to the POJO:
{
"error":[
{
"code": 1,
"message":"Parameter is invalid."
}
]
}
UPDATE:
The response format for a valid call looks something like this:
persons: {
personCount: 14
person: [
{
firstname: "Michael"
name: "Bolton"
}
]
}
I'm mapping the response like this:
Person person = mapper.readValue(in, Person.class);
I've looked through the API and some SO and blog-postings, but I didn't find a hint on how to approach this so far. Can you give me a hint?
Related
My response as below and I want to convert it to json object but I don't know how to do it. Could you guide me? Thank you!
Response:
{"m_list": "[{\"contract\":{\"category\":1,\"cor_num\":101,\"contract_name\":\"ABC\"},\"bu_unit\":{\"bu_name\":\"1-1E\"}}]"}
My expected => It'll convert as a json object as below
{ m_list:
[ { contract:
{ category: 1,
cor_num: 101,
contract_name: 'ABC'},
bu_unit: { bu_name: '1-1E' }} ] }
I tried the following way but seem it didn't work
JSONObject jsonObject = new JSONObject(str)
You can use library Josson to restore the JSON object/array from a string inside a JSON.
https://github.com/octomix/josson
Deserialization
Josson josson = Josson.fromJsonString(
"{\"m_list\": \"[{\\\"contract\\\":{\\\"category\\\":1,\\\"cor_num\\\":101,\\\"contract_name\\\":\\\"ABC\\\"},\\\"bu_unit\\\":{\\\"bu_name\\\":\\\"1-1E\\\"}}]\"}");
Transformation
JsonNode node = josson.getNode("map(m_list.json())");
System.out.println(node.toPrettyString());
Output
{
"m_list" : [ {
"contract" : {
"category" : 1,
"cor_num" : 101,
"contract_name" : "ABC"
},
"bu_unit" : {
"bu_name" : "1-1E"
}
} ]
}
The string you want to convert is not in the JSON format. From the official documentation of JSON https://www.json.org/json-en.html - it has to start with left brace { , and end with right brace }.
Following the comment from #tgdavies if you get the response from some server, ask for clarification. If you just do this yourself, then this string is the correct format for the json file you want.
{"m_list":[{"contract":{"category":1,"cor_num":101,"contract_name":"ABC"},"bu_unit":{"bu_name":"1-1E"}}]}
I want to create a functionality in code when I get JSON request and convert the fields of this JSON to another one JSON object based on fields mapping in DB.
For example:
Request Json
{
{
"param1": "value1",
"param2": "value2",
"data": {
"param3": "value3"
}
}
Table in DB:
param1 | newMappedParam1
param2 | newMappedParam2
param3 | newMappedParam3
New generated Json object:
{
{
"newMappedParam1": "value1",
"newMappedParam2": "value2",
"data": {
"newMappedParam3": "value3"
}
}
I can create POJO model object and then just to manipulate with this. But when the new parameter is added, I need also to update the POJO model and it requires to release a new version of application. This is not desired option.
I think on another approach like getting the JSON request , on the fly I convert to some JSONObject and then I map and build anew Json object. And when i'm required to add the new parameter , i just add the record in DB and no more new releases for this.
Do you think this approach is very good for this?
Thanks
I am in the process of converting the main JSON library for my application from net.sf to Jackson.
For my REST endpoints, before the change of libraries, I was generating a Response object like this:
...
JsonArray arr = JSONArray.fromObject(obj);
return Response.ok(arr).build();
After switching to Jackson, that same code looked like this:
...
ObjectMapper mapper = new ObjectMapper();
ArrayNode arr = mapper.valueToTree(obj);
return Response.ok(arr).build();
However, when I query this rest endpoint now, the JSON looks like this:
{
"_children": [
{
"_children": {
"#id": {
"_value": "myID"
}
}
}
]
}
instead of
[
{
"#id": "myID"
}
]
Why is this happening? It is breaking the front end, because the front end is not expecting the JSON to be structured that way. My only workaround so far has been to do an arr.toString() on the ArrayNode object inside the Response.ok() call.
I am working on a module where i am getting a JSON response from a RESTful web service. The response is something like below.
[{
"orderNumber": "test order",
"orderDate": "2016 - 01 - 25",
"Billing": {
"Name": "Ron",
"Address": {
"Address1": "",
"City": ""
}
},
"Shipping": {
"Name": "Ron",
"Address": {
"Address1": "",
"City": ""
}
}
}]
This is not the complete response, but only with important elements just to elaborate the issue.
So what i need to do is, convert this JSON response into another JSON that my application understands and can process. Say the below for example.
{
"order_number": "test order",
"order_date": "2016-01-25",
"bill_to_name": "Ron",
"bill_to_address": "",
"bill_to_city": "",
"ship_from_name": "Ron",
"ship_from_Address": "",
"ship_from_city": ""
}
The idea that i had tried was to convert the JSONObject in the response i receive to a hashmap using JACKSON and then use StrSubstitutor to replace the placeholders in my application json with proper values from response json(My Application string with placeholders Shown below).
{"order_number":"${orderNumber}","order_date":"${orderDate}","bill_to_name":"${Billing.name}","bill_to_address":"${Billing.Address}","bill_to_city":"${Billing.City}","ship_from_name":"${Shipping.Name}","ship_from_Address":"${Shipping.Address}","ship_from_city":"${Shipping.City}"}
But the issue i faced was that
JSON to MAP didn't work with nested JSONOBJECT as shown in the response above.
Also to substitute Billing.Name/Shipping.Name etc, even if i extract the Shipping/Billing JSONObjects from the response, when i
would convert them to hashmap, they would give me Name, City,
Address1 as keys and not Billing.Name, Billing.City etc.
So as a solution i wrote the below piece of code which takes the response JSONObject(srcObject) and JSONObject of my application(destObject) as inputs, performs processing and fits in the values from the response JSON into my application JSON.
public void mapJsonToJson(final JSONObject srcObject, final JSONObject destObject){
for(String key : destObject.keys()){
String srcKey = destObject.getString(key)
if(srcKey.indexOf(".") != -1){
String[] jsonKeys = srcKey.split("\\.")
if(srcObject.has(jsonKeys[0])){
JSONObject tempJson
for(int i=0;i<jsonKeys.length - 1;i++){
if(i==0) {
tempJson = srcObject.getJSONObject(jsonKeys[i])
} else{
tempJson = tempJson.getJSONObject(jsonKeys[i])
}
}
destObject.put(key, tempJson.getString(jsonKeys[jsonKeys.length - 1]))
}
}else if(srcObject.has(srcKey)){
String value = srcObject.getString(srcKey)
destObject.put(key, value)
}
}
}
The issue with this piece of code is that it takes some time to process. I want to know is there a way i can implement this logic in a better way with less processing time?
You should create POJOs for your two data types, and then use Jackson's mapper to deserialize the REST data in as the first POJO, and then have a copy constructor on your second POJO that accepts the POJO from the REST service, and copies all the data to its fields. Then you can use Jackson's mapper to serialize the data back into JSON.
Only if the above still gives you performance issues would I start looking at faster but more difficult algorithms such as working with JsonParser/JsonGenerator directly to stream data.
I feel the standard approach will be to use XSLT equivalent for JSON. JOLT seems to be one such implementation. Demo page can be found here. Have a look at it.
I am writing Junit test case for my REST service, i am setting the values(below is the piece of code) to get the payload required for REST service
Payload jsonPayload = new Payload ();
payload.setAcc("A");
List<Details> details= new ArrayList<Details>;
Details detail = new Details();
detail.setTotalAmount(1);
detail.setCurrency("dollar");
details.add(detail);
payload.getDetails().addAll(details);
I want JSON to be built in format mentioned below, but I am not getting the JSON as expected, details should be in form of Array.
Required JSON -
{
"Acc" : "A",
"details": [
{
"totalAmount":1,
"currency":"dollar"
}
]
}
Output JSON -
{
"Acc" : "A",
"details":
{
"totalAmount":1,
"currency":"dollar"
}
}
Can anyone help me how can I achieve this?