1
votes

my json is in below format and for some reason i have to convert it to a string using jackson.

{
"responseCode": 200,
"output": [
    {
        "address": {
            "address1": "string",
            "address2": "string",
            "addressId": "1543",
            "city": "string",
            "fxGeocode": "string",
            "houseNumber": "string",
            "isActive": true,
            "postalCode": "string",
            "state": "string",
            "streetName": "string",
            "zip": "string"
        },
        "city": "string",
        "externalIds": [
            {
                "externalId": "string",
                "externalIdDocId": "676273d0-ebf7-4f46-bd5a-e8af09589646",
                "partyRoleExternalIdType": "string"
            }
        ],
        "name": "string",
        "organizationRoleId": 975,
        "zip": "string"
    },
    {
        "address": {
            "address1": "string",
            "address2": "string",
            "addressId": "1767",
            "city": "string",
            "fxGeocode": "string",
            "houseNumber": "string",
            "isActive": true,
            "postalCode": "string",
            "state": "string",
            "streetName": "string",
            "zip": "string"
        },
        "city": "string",
        "externalIds": [
            {
                "externalId": "string",
                "externalIdDocId": "248badd9-8b5d-4b9d-8c00-055f0afacdfc",
                "partyRoleExternalIdType": "string"
            }
        ],
        "name": "string",
        "organizationRoleId": 976,
        "zip": "string"
    }
],
"totalCount": 2
}

After unmarshalling it to string using jackson it is converted in string as below.

{output=[{address={address1=string, address2=string, addressId=1543, city=string, fxGeocode=string, 
 houseNumber=string, isActive=true, postalCode=string, state=string, streetName=string, zip=string}, 
 city=string, externalIds=[{externalId=string, externalIdDocId=676273d0-ebf7-4f46-bd5a-e8af09589646, 
 partyRoleExternalIdType=string}], name=string, organizationRoleId=975, zip=string}, {address= 
{address1=string, address2=string, addressId=1767, city=string, fxGeocode=string, 
 houseNumber=string, isActive=true, postalCode=string, state=string, streetName=string, zip=string}, 
 city=string, externalIds=[{externalId=string, externalIdDocId=248badd9-8b5d-4b9d-8c00-055f0afacdfc, 
 partyRoleExternalIdType=string}], name=string, organizationRoleId=976, zip=string}], 
 responseCode=200, totalCount=2}

how to i convert it back to the json format in java? Should i create a class for the same to marshal it back?

1
either you can create a Class and marshal it again or marshal it to a map.dassum
how can i marshal it to a map?rajkumar.11
You need to show how do you deserialised it. {output=[{address ... looks like a result of toString method invoked on Map.Michał Ziober
@michalZiober <convertBodyTo id="_convertBody11To21" type="java.lang.String"/> <unmarshal id="abc" ref="jackson"/> where jackson is=> <dataFormats> <json id="jackson" library="Jackson" unmarshalTypeName="java.util.TreeMap"/> </dataFormats> so map of json is directly converted to string using jacksonrajkumar.11

1 Answers

0
votes

Option 1. Create a Java Class and Marshal it to Java Class.

Option 2. Marshal it to HashMap

Map<String, Object> map = new HashMap<String, Object>();

//Convert Map to JSON

 map = new ObjectMapper().readValue(json, new TypeReference<Map<String, Object>>(){});