Sunday, December 23, 2018

JSON Parser

PeopleSoft has undocumented JSON related API and this post covers some of the routines that I have tried to dynamically parse a json response. I think this API calls were made available in PT 8.56.x as part of PeopleSoft's inbuilt integration with ElasticSearch. 

I am making a RESTful web service call to a service hosted by a 3rd party vendor from PeopleSoft; so PeopleSoft is a consumer of the service. Focus here is to parse the response from the service so I am not covering how the service is setup and the request part of the service. 

So I have two types of responses. Response 1 as shown below 


{

    "error": {
        "message": "Some message text",
        "detail": "detail text about the error"
    },
    "status": "failure"
}

and Response 2 as follows.

{
   "import_set":"Import set value",
   "staging_table":"tablename",
   "result":[
      {
         "status":"updated",
         "error_message":"Unable to format 01-01-2019 using format string yyyyMMdd  for field hire_dt"
      }
   ]
}

In Response 1, I has 2 children, viz "error" and "status", whereas Response 2 has 3 children, import_set, staging_table and result.

In Response 1, "error" is a JSonObject which has 2 more children, message and detail. In Response 2, "result" is a JSonArray which has 2 children status and error_message.


Local string &content, &propName, &propValue;
Local JsonParser &parser;
Local JsonObject &jsonRoot, &jsonDetails;
Local JsonArray &jArray;
Local boolean &ret; 
Local number &i, &j, &k, &l;


&parser = CreateJsonParser(); /* this is the undocumented API */
&ret = &parser.Parse(&content); /* &content is the json response as a string */
&jsonRoot = &parser.GetRootObject();

For &i = 1 To &jsonRoot.GetChildCount()
/* for Response 1, following will get status tag and its value */
/* for Response 2, following will get import_set, staging_table and its values */
   &propName = &jsonRoot.GetPropertyNameAt(&i);
   &propValue = &jsonRoot.GetProperty(&propName);
   /* if there is a nested value then its either JsonArray or JsonObject */
   Evaluate &propValue
   When = "JsonArray"
/* this will return status and error_message which are in Response 2 */
      &jArray = &jsonRoot.GetJsonArray(&propName);
      For &j = 1 To &jArray.Length()
         &jsonDetails = &jArray.GetJsonObject(&j);
         For &k = 1 To &jsonDetails.GetChildCount()
            &propName = &jsonDetails.GetPropertyNameAt(&k);
            &propValue = &jsonDetails.GetProperty(&propName);
         End-For;
      End-For;
      Break;
   When = "JsonObject"
/* this will return message and detail which are in Response 1 */
      &jsonDetails = &jsonRoot.GetJsonObject(&propName);
      &numCnt = &jsonDetails.GetChildCount();
      For &l = 1 To &jsonDetails.GetChildCount()
         &propName = &jsonDetails.GetPropertyNameAt(&l);
         &propValue = &jsonDetails.GetProperty(&propName);
      End-For;
      Break;
   When-Other;
      /* when not JsonArray or JsonObject, get prop name and value which is at root level */
      Break;
   End-Evaluate;
End-For;

Hope this helps.

10 comments:

  1. [
    {
    "Code": "3859",
    "TypeCode": "COST",
    "operationType": "UPDATE",
    "result": "SUCCESS"
    },
    {
    "Code": "6248",
    "TypeCode": "TYPEX",
    "errorMessage": "Invalid Type Code",
    "result": "FAIL"
    }
    ]

    Can you please help in parsing the response in peoplecode. Also based on result , I need to do some processing

    ReplyDelete
    Replies
    1. You can use the "JsonArray" flow that I have mentioned in my post.

      Delete
  2. Hi Deepak,

    I am trying to hide the object label of my child document but it doesnt seem to work. Any ideas on this?

    Thanks!

    ReplyDelete
  3. {
    "Test": [
    {
    "STUDENT_ID": "12344",
    "STUDENT_NAME": "EAAI BAMA IAA"
    },
    {
    "STUDENT_ID": "123456",
    "STUDENT_NAME": "HBEM KCMA ECAD"
    }
    ]
    }

    The "Test" label wont seem to go away even if the Hide Parent Object Label is already ticked in the Document Builder page. For a million tries of saving and validating the document, sometimes it works but most of the time it doesnt. Also sometimes some junk text will appear either in the header or the field label. Have you experienced this before?

    ReplyDelete
    Replies
    1. Sorry for the late reply, I don't think "Hide Parent Object Label" will work as "Test" is not the parent object, it is actually a compound.

      Delete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. How did you do the service setup without document configuration. I am looking forward to do this along with service setup. Could you post that pls ?

    ReplyDelete
    Replies
    1. PS coder,
      Take a look at my post https://psdips.blogspot.com/2020/04/creating-json-request-for-rest-web.html and I think that is what you are looking for.

      Delete
  6. Thanks for this Blog.. Helped to sort out Parsing Logic for Jason Message.. I did extend it for another 2 Levels and made a generic framework..

    ReplyDelete