-2

Problem using Jackon ObjectMapper to read a String created from a invalid JSON response

Here is the relevant snippet of the code:

Response response = request.get();  
String responseAsString = response.readEntity(String.class);
ObjectMapper mapper = new ObjectMapper();
JsonNode responseNode = mapper.readTree(responseAsString);

When I run this code I get this exception: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('-' (code 45)): was expecting comma to separate OBJECT entries

Background on the problem: I am making a GET request and it is successfully responding. I then convert that response into a String so that I can then change it into a JsonNode and extract the parts I want. If I print out the responseAsString it looks like this:

{
    "account": 123456789,
    "balance": 5602,
    "dateTime": 2017-06-15T03:29:00,
    "token": "H77ABC0PPIQ"
}

I believe the issue has to due with how the response is formed since not all of the values are sent as Strings. Their types are as follows:

account: long
balance: long
dateTime: DateTime
token: String

I'm not sure how to convert the response to a Json object so that I can get individual values like the account number for example.

Any advice is appreciated. Thanks!

SuperCow
  • 1,523
  • 7
  • 20
  • 32
  • 1
    That's invalid JSON. The date should be surrounded by quotes. – Sotirios Delimanolis Jul 27 '17 at 00:04
  • Right, maybe I phrased my question incorrectly then. When I get the response and run toString on it that is what I get -- invalid JSON. How can I use object mapper on it or make it valid JSON? Thank.s – SuperCow Jul 27 '17 at 01:52
  • 1
    ObjectMapper parses JSON. If what you have is not JSON, then it cannot parse it. Go to the source and fix your JSON. Or use something else that can parse this non-JSON. – Sotirios Delimanolis Jul 27 '17 at 02:44
  • I cannot change the response I receive. I mean I can manually convert the dateTime object into a String and then use the ObjectMapper. Or is there something else you can suggest to parse this non-JSON? – SuperCow Aug 01 '17 at 23:31

1 Answers1

1

The issue is with badly formatted JSON. You can use tools like JSONlint to verify the JSON. You should check how request's payload you are receiving is being marshaled. With a proper JSON in the request, your code would work just fine.

As a quick test, i will use ObjectMapper to serialize, print , deserialize and then navigate an account object.

Domain class (i'm using Lombok for brevity )

@Data
@AllArgsConstructor
private class Account {
    private Integer account;
    private Integer balance;
    private String dateTime;
    private String token;

}

Test:

@Test
public void test() throws IOException {

    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    String responseAsString = mapper.writeValueAsString(new Account(123456789,5602,"2017-06-15T03:29:00","H77ABC0PPIQ"));
    System.out.print(responseAsString);
    JsonNode responseNode = mapper.readTree(responseAsString);

    assertEquals("2017-06-15T03:29:00",responseNode.get("dateTime").textValue());
}

JSON generated by serializing the account object :

{
  "account" : 123456789,
  "balance" : 5602,
  "dateTime" : "2017-06-15T03:29:00",
  "token" : "H77ABC0PPIQ"
}
diy
  • 3,590
  • 3
  • 19
  • 16
  • Thanks for the response. The issue I'm seeing is when you're creating a new account, how would you be getting those arguments for new Account from the response I have? – SuperCow Jul 27 '17 at 01:56