Raghav
Raghav

Reputation: 552

Improving processing time for mapping one json object to another

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

  1. JSON to MAP didn't work with nested JSONOBJECT as shown in the response above.

  2. 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?

Upvotes: 0

Views: 1574

Answers (2)

Mahesha999
Mahesha999

Reputation: 24721

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.

Upvotes: 0

Darth Android
Darth Android

Reputation: 3502

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.

Upvotes: 1

Related Questions