1

Hi I am trying to return a second value pair (string) from a JSON object using APEX's own built-in parser. The API query takes a string variable of a web domain.

However, I am getting a strange error I've never seen before:

Line: 7, Column: 0
expecting a semi-colon, found '<EOF>'

Here's my code:

 public class companyIDFetcher {

    public string companyIDFetcher (string domain){ 
        HTTPRequest companyIDRequest = new HTTPRequest();
        companyIDRequest.setEndpoint('https://api.api.com/api/v0/something/something/id?domain_name=' + domain);
        companyIDRequest.setMethod('GET');

        HTTP n = new HTTP();
        HTTPResponse companyIDResponse = n.send(companyIDRequest);
        string MosaicAPIBasicRequest = companyIDResponse.getBody();

        JSONParser parser = JSON.createParser(MosaicAPIBasicRequest);
        parser.nextToken();
        parser.nextToken();
        parser.nextValue();
        String companyID = parser.getText();
        system.debug(companyID);
        return companyID;
    }

Any advice would be appreciated.

Here's a JSON Response that I am receiving when making a http request:

{
  "meta": {
    "number_of_records": 3
  },
  "data": "13458245"
}
  • when are you getting the error? While compiling or while running the code? – Guy Jun 2 '17 at 9:23
  • are you running it in anonymous apex ? you have trash symbol before "public" access modifier word. Remove it. – Oleksandr Berehovskyi Jun 2 '17 at 9:30
  • I am getting the error while running the code. Trash symbol removed. I am running in anonymous APEX. – Kamil Mieczakowski Jun 2 '17 at 9:44
  • Did you check the JSON request, since it might be possible there is no second pair and you have reached the object end. Can u also post the JSON response, which you receiving before parsing ? – anmrk Jun 2 '17 at 9:53
  • anmrk: I just pasted the JSON response above. – Kamil Mieczakowski Jun 2 '17 at 9:55
0

try this, Probably you need to change it with the correct field name

String response = '{"meta": {"number_of_records": 3},"data": "13458245"}';

JSONParser parser = JSON.createParser(response);
while (parser.nextToken() != null) {
    if(parser.getCurrentName() == 'number_of_records') {
        parser.nextValue();
        System.debug(parser.getDecimalValue());
    }
}
  • ok. in that case just check whether the json received is in correct format using json validator service found online. also only parse the response if the status code is 200 – anmrk Jun 2 '17 at 10:48
  • json validator service reports it as valid – Kamil Mieczakowski Jun 2 '17 at 10:56
0

This kind of changes what you are doing, but have you used JSON.deserialize() before? (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_Json.htm)

I prefer it as it allows you to separate a "Model" from your functionality. You could therefore do the following:

public class MosaicAPIBasicRequest {
    public Meta meta;
    public String data;

    public class Meta {
        public Integer number_of_records;
    }
}

With your original code changed to:

public class companyIDFetcher {

    public string companyIDFetcher (string domain){ 
        HTTPRequest companyIDRequest = new HTTPRequest();
        companyIDRequest.setEndpoint('....');
        companyIDRequest.setMethod('GET');

        HTTP n = new HTTP();
        HTTPResponse companyIDResponse = n.send(companyIDRequest);
        MosaicAPIBasicRequest request = (MosaicAPIBasicRequest) JSON.deserialize(companyIDResponse.getBody(), MosaicAPIBasicRequest.class);

        String companyID = request.data;
        system.debug(companyID);
        return companyID;
}
  • 1
    Thank you Jon but I am getting exactly the same error using this method... Line: 7, Column: 0 expecting a semi-colon, found '<EOF>' – Kamil Mieczakowski Jun 2 '17 at 10:09
  • I'm not sure then, it seems like the response is missing a closing bracket '}' or some other syntax character. I would guess that the error is in the JSON you are requesting. – Jon Duelfer Jun 2 '17 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.