Topic
2 replies Latest Post - ‏2013-08-23T13:58:18Z by JBASkeen
JBASkeen
JBASkeen
137 Posts
ACCEPTED ANSWER

Pinned topic JSON Array Conversion to EGL Record

‏2013-07-23T22:18:58Z |

I am attempting to parse a JSON response which contains arrays as the main objects. However, I am finding that the ServiceLib.convertFromJSON method does not handle these cases within my scenario. I am using a third-party API call which returns JSON responses this way. From my understanding JSON can be formatted into arrays per JSON standards; an article discussing this topic is here: http://stackoverflow.com/questions/5034444/can-a-json-start-with

I am trying to find out if ServiceLib.convertFromJSON should take this JSON input or not.

Here is a sample of what I am trying to do:

{code}

service myService
    
    function testJSON()
        try
            json string = "[{\"name\" : \"James\"}]";
            userRec userRec;
            ServiceLib.convertFromJSON(json,userRec);
        onException(exp AnyException)
            msg string = exp.message;
        end
    end

end

record userRec type BasicRecord
    user userAttribs[];
end

record userAttribs type BasicRecord
    name string{JSONName = "name"};
end

{code}

 

Error:

(string) "EGL1542E An exception occurred while converting from JSON. parameter:userRec, json:[{"name" : "James"}]
EGL0002I The error occurred in myService processing the testJSON function.
 Encountered " "[" "[ "" at line 1, column 1.
Was expecting:
    "{" ...
    "

Thanks in advance,

James Skeen

RBD 8.5.1

  • HuangJiYong
    HuangJiYong
    88 Posts
    ACCEPTED ANSWER

    Re: JSON Array Conversion to EGL Record

    ‏2013-08-23T01:42:39Z  in response to JBASkeen

    Hi James,

     

    I know you have opened a PMR and get our feedback. I will paste the reply here for others who find this thread.

    The JSON string is valid. But the the json string cannot be mapped to your record. In the testcase, ServiceLib.convertFromJSON(json,userRec)
    userRec has a field "user" which is not mapped in the json string, so the conversion fails.
    In fact, ServiceLib.convertFromJSON does not support array as the second parameter, only record or dictionary is allowed as specified in the doc http://pic.dhe.ibm.com/infocenter/rbdhelp/v8r5m0/index.jsp?topic=%2Fcom.ibm.egl.lr.doc%2Ftopics%2Fregl_core_service_convert_from_json.html
     
    In this case the user can easily convert an array json string to a object json string and then use ServiceLib.convertFromJSON.  To match the record definition, the json string should be json string = "{\"user\" : [{\"name\" : \"John\"},{\"name\" : \"Joe\"}]}"; You can make some modification to the response by adding {\"user\" :  before the response, and } after the response to make it matched with the record.
    Thanks.

    -Jiyong

     

    • JBASkeen
      JBASkeen
      137 Posts
      ACCEPTED ANSWER

      Re: JSON Array Conversion to EGL Record

      ‏2013-08-23T13:58:18Z  in response to HuangJiYong

      Jiyong,

      Thanks for your reply and thanks if you assisted with the PMR. I have marked this thread as answered. Manipulating the JSON string worked well.

      James Skeen