1

I´m trying to complete a trailhead challenge but when I test the method, it appears the error

System.JSONException: Unexpected character ('a' (code 97)): was expecting double-quote to start field name at [line:1, column:3]

This is the json

{"animal":{"id":0,"name":"","eats":"","says":""}}

Also, when I run the same code on Execute Anonymous, it does work! So I don't know what else to do.

public class AnimalLocator {

public static String getAnimalNameById(integer id){
    Http http = new Http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/' + id);
    request.setMethod('GET');
    HttpResponse response = http.send(request);

    Map<String,Object> result = (Map<String,Object>)JSON.deserializeUntyped(response.getBody());
    Map<String,Object> animal = (Map<String,Object>) result.get('animal');
    return (String)animal.get('name');
}

}

@isTest
public class AnimalLocatorTest {

@isTest static void testAnimalLocator(){
    Test.setMock(HttpCalloutMock.class, new AnimalLocatorMock());

    String name = AnimalLocator.getAnimalNameById(0);

    System.assertEquals(name, 'Alex');
}

}

The error seems to be here Map<String,Object> animal = (Map<String,Object>) result.get('animal');

UPDATE The error was in the CalloutMock class, I was trying to create a JSON without the double quotes.

public HttpResponse respond(HttpRequest request){
    HttpResponse response = new HttpResponse();
    response.setBody('{"animal": {"id": 0, "name": "Alex", "eats": "", "says": ""}}');
    response.setStatusCode(200);
    return response;
}

2 Answers 2

4

This isn't valid JSON:

{
  animal: {
    id: 0,
    name: "",
    eats: "",
    says: "",
  }
}

and the error actually says exactly what's wrong:

Unexpected character ('a' (code 97)): was expecting double-quote to start field name at [line:1, column:3]

JSON keys are required to be quoted strings. You must quote "animal", "id", "name", "eats", and "says".

2
  • how can I do that in Apex? I'm aware that double quotes are not permitted.
    – Alex Torre
    Apr 21, 2020 at 22:43
  • You can use double quotes inside a single-quoted Apex string literal, if that's how you're constructing this. You might also check out static resource mocks, which are a neat way to store and produce JSON for use in HTTPCalloutMocks.
    – David Reed
    Apr 21, 2020 at 22:59
0

I receive errors with the following JSON (provided by https://developer.salesforce.com/docs/atlas.en-us.cpq_dev_api.meta/cpq_dev_api/cpq_api_add_products.htm)

{"context":"{"quote":{"record":{"attributes":{"type":"SBQQ__Quote__c","url":"/services/data/v41.0/sobjects/SBQQ__Quote__c/a0p61000004IpR8AAK"}, "Name":"Q-00905","Id":"a0p61000004IpR8AAK"},"nextKey":2,"netTotal":0.00,"lineItems":[],"lineItemGroups":[], "customerTotal":0.00},"products":[],"groupKey":0, "ignoreCalculate": true}"}

Initially it gave an error regarding context "Unexpected parameter encountered during deserialization: context at [line:1, column:13]" However after adding escapes to the context it brings up errors similar to this pre-existing issue here on stackoverflow

": Unexpected character ('' (code 92)): was expecting double-quote to start field name at [line:1, column:3]"

As quotes are provided in the JSON, should this work or are there additional changes required?

Thanks for any help on this.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .