user09
user09

Reputation: 956

String to Json converter is giving invalid Json value

Hi I have a field in my Json request for it's value is a Json. So I am converting string to Json applying escape characters. Again this converted string is going through a Object to Json transformer and converting it is a invalid Json.

What I need:

"attributes" : {"type" : "CARES_Diagnosis_Survey__c", "referenceId" : "ref1"},

What I am doing:

public static final String ATTRIBUTES_BEGIN = "{\"type\"" +":"+ "\"CARES_Diagnosis_Survey__c\""+","+"\"referenceId\"" +":"+ "\"ref";
public static final String ATTRIBUTES_END = "\"}";
String attributes = ServiceConstants.ATTRIBUTES_BEGIN + ServiceConstants.ATTRIBUTES_END;
salesForceDiagnosisObject.setAttributes(attributes);

This is salesForceDiagnosisObject is going through Object to Json transformer in spring integration 

<!--  turn to json -->
    <int:object-to-json-transformer id="sfdcDiagnosisOutboundToJsonTransformer"
      input-channel="sfdcDiagnosisObjectToJsonConverter" 
      output-channel="sfdcDiagnosisOutboundToJson"
      />

What I am getting:

"attributes":"{\"type\":\"CARES_Diagnosis_Survey__c\",\"referenceId\":\"ref\"}"

What I want to get:

"attributes" : {"type" : "CARES_Diagnosis_Survey__c", "referenceId" : "ref1"}

I have tried to use JSonIgnore on this field to skip the serialization but it is completely omitting the field if I did. Please help me.

Upvotes: 0

Views: 171

Answers (2)

minus
minus

Reputation: 2786

You are possibly doing it the wrong way, you do not map a string to JSON, you map an Object to JSON.

So if you want your salesForceDiagnosisObject to be serialized as a JSON containing the attributes like this:

{ 
  "key1" : "value1",
  "key2" : "value2",
....
  "attributes" : {
    "type" : "CARES_Diagnosis_Survey__c", 
    "referenceId" : "ref1"
} 

You salesForceDiagnosisObject class can't be:

class SalesForceDiagnosisObject {
  String key1;
  String key2;
  String attributes;
   ....
}

It must be something like this:

class SalesForceDiagnosisObject {
  String key1;
  String key2;
  DiagnosisAttribute attributes;
   ....
}

and your attributes should be set like this:

class DiagnosisAttribute{
  String type;
  String referenceId;
 ...
}

DiagnosisAttribute da = new DiagnosisAttribute();
da.setType("CARES_Diagnosis_Survey__c");
da.setReferenceId("ref1");

salesForceDiagnosisObject.setAttributes(da);

Upvotes: 2

Darshan Mehta
Darshan Mehta

Reputation: 30819

Your String looks correct, it's just the backward slashes (used to escape double quotes) that make the string look a bit odd. However, if you sysout the resultant string, it does not include slashes.

Below is an example code that converts attributes into a Map using ObjectMapper:

public static void main(String[] args) throws Exception{
    String ATTRIBUTES_BEGIN = "{\"type\"" +":"+ "\"CARES_Diagnosis_Survey__c\""+","+"\"referenceId\"" +":"+ "\"ref";
    String ATTRIBUTES_END = "\"}";
    String attributes = ATTRIBUTES_BEGIN + ATTRIBUTES_END;
    ObjectMapper mapper = new ObjectMapper();
    Map<String, Object> value = mapper.readValue(attributes, new TypeReference<Map<String,Object>>() {});
    System.out.println(value);
}

Upvotes: 1

Related Questions