0

I am trying to pull the values from a JSON response and have managed to get most of the values except the one I need which is nested in an inner set of curly brackets

public class web_ser {

@future(callout=true)
public static void web_services_rest(String order_JSON){

    String payload_JSON =  order_JSON;
    String endpoint = 'https://some_website.com';

    HttpRequest request = new HttpRequest();
    Http http = new Http();

    request.setEndpoint(endpoint);
    request.setMethod('POST');
    request.setBody(payload_JSON);

    HttpResponse response = http.send(request);

    System.debug(response.toString());
    System.debug('STATUS: ' + response.getStatus());
    System.debug('STATUS_CODE: ' + response.getStatusCode());

    String responseJSON = response.getBody();
    System.debug('responseJSON: ' + responseJSON);

    Map<String, Object> deserialized_JSON_response = 
        (Map<String, Object>)JSON.deserializeUntyped(responseJSON);

    Object message = deserialized_response.get('message');
    System.debug('message: ' + message);
}}

The output from message looks something like this:

responseJSON: {"status": "100", "code": "success", "message": "{order_number:00001979,id_number:I-00000982-10}"}

The information I'm trying to pull, id_number, is in the message portion of the deserialized JSON packet:

"message": "{order_number:00001979,id_number:I-00000982-10}"

I am able to to put message into a variable but have been unable to extract the value for the key id_number. What am I missing to be able to access id_number?

2
  • Probably worth a look at the example here in JSON.deserializeUntyped() and specifically look at how they retrieve the dimensions attribute data - which is similar to what you are requesting above. – Mark Pond Dec 5 '19 at 22:22
  • @MarkPond I tried following that example but all my system debugs start to fail. 'Map<String, Object> essage = (Map<String, Object>) deserialized_response.get('message'); System.debug('message: ' + message);' My system debug does not print at all and any system debug after, seem's like, is dropped. – Richard Dec 5 '19 at 23:00
3

Because the interior value(s) of message is not JSON:

"message": "{order_number:00001979,id_number:I-00000982-10}"

you'll need to use string class methods as in:

String IdNumber = messageVbl.substringBetween('id_number:','}');
5
  • I get the following error 'Method does not exist or incorrect signature: void substringBetween(String, String) from the type Map<String,Object>' – Richard Dec 5 '19 at 22:55
  • 1
    All sorts of ways to get that value out... you could use a regex match or string slicing using index of etc. But with the example above you need to first get the message value out of the untyped parsed JSON map into a string... that is what messageVbl clearly is. – Phil W Dec 5 '19 at 23:01
  • @Richard, I'm assuming you have already extracted the message property into some string variable; if it is still in a map, you'll need to fetch from the map – cropredy Dec 5 '19 at 23:19
  • @PhilW and cropedy Thank you guys! I got it working properly – Richard Dec 6 '19 at 0:19
  • 1
    I didn't even notice that the content wasn't a JSON object! @Richard if you've got the ability to get it changed, I would ask the developer of the remote endpoint to send you back proper JSON if they can do so. (Future developers will thank you) – Mark Pond Dec 6 '19 at 0:50

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.