1

I'm parsing a JSON string I receive from an an external webservice and I'm getting "System.JSONException: Unexpected character ('S' (code 83)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at input location [1,2]" here's the JSON being parsed:

{"authenticationToken": "DQXSABQZDTTDSHPEON1EC5O2JK8KBZ31",
"_links": {"st:node": {"href": "/twig-cstc3/v1/login"},
"curies": [{"name": "st","href": "https://web210.ntree.com/twig-cstc3/v1/rels/{rel}","templated": true}]},
"_embedded": {"node": {
  "name": "Steve Placey",
  "description": "Placey, S. (6909)",
  "nodeCode": "6909",
  "_links": {"self": {"href": "/twig-cstc3/v1/login"},
    "events": {"href": "https://web210.ntree.com/twig-cstc3/v1/node/14604/calendar"},
    "st:calendar": {"href": "https://web210.ntree.com/twig-cstc3/v1/node/14604/calendar"},
    "st:today": {"href": "https://web210.ntree.com/twig-cstc3/v1/node/14604/calendar?from=2015-08-26&to=2015-08-26","title": "Today's events"},
    "search": {"href": "https://web210.ntree.com/twig-cstc3/v1/vault/search","title": "Vault Search"}
  }}}}

I can see where it's choking, it's the S. in the description block. All the records this call returns are going to have an initial in that block. Here's the code I'm using for the parser:

JSONParser parser = JSON.createParser(res.getBody());
while (parser.nextToken() != null) {
    if (parser.getCurrentToken() == JSONToken.START_ARRAY) {
        while (parser.nextToken() != null) {
            if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && parser.getText() == 'authenticationToken')) {
                // Get the value.
                parser.nextToken();
                xAuthToken = parser.getText();
                if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'st:today')){
                    parser.nextToken();
                    parser.nextToken();
                    st_Today = parser.getText();

Is there a way to get it to skip the initial in description block without having to code an if statement for every letter in the alphabet?

  • 1
    I usually create a wrapper class and de-Serialize the JSON string in Apex object. just to mention, I have got the error when I try to beautify the json or create the apex object from json. – amidstCloud Aug 26 '15 at 17:13
  • is the JOSN is proper, I tried to format above JSON and got invalid JSON? – San Aug 26 '15 at 17:17
  • You can paste the JSON into e.g. jsonformatter.curiousconcept.com and check it. Errors start here "curies": ["name": "st" because of the [ instead of a { (or a missing {). Also if the JSON is fixed, json2apex.herokuapp.com will generate a ready to use class to parse and represent the data - much less work and more reliable than writing your own parser. – Keith C Aug 26 '15 at 17:47
  • @KeithC The issues with the JSON are a copy and paste error on my part, and not an issue with the original JSON, it appears when I tightened it up I delete the curly bracket by mistake. I've edited the post to replace the missing curly bracket – Tim Aug 26 '15 at 19:36
  • The issue is somewhere in your parser, not the JSON string. I've tried your parser in exec anon window with a simple JSON string, and it still throws an error. Unfortunately, I don't have time to dig deeper at the moment, but I would suggest to try not nesting loops within loops. – dino ursic Sep 8 '15 at 5:46
0

You can convert your list into String using JSON.serialize() and than use JSON.deseriealize() to convert it into wrapperlist.

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.