Question
Commonwealth Bank of Australia
AU
Last activity: 18 Aug 2015 10:53 EDT
Parsing a JSON Payload
Hi Team,
I have a requirement where we need to create cover and covered objects from an input payload that is received in the form of a JSON file.
Ideally, we should be having a Service-Rest exposed from Pega which shall send the data received to an activity that shall parse the data from JSON and set it to the clipboard.
But, I am not sure how to parse the JSON data.
As, we don't have an accelerator for the same the data model for the same also needs to be created manually I suppose but I am stuck with the parsing logic.
Please help
-
Like (0)
-
Share this page Facebook Twitter LinkedIn Email Copying... Copied!
Accepted Solution
Pegasystems Inc.
GB
Hi Subhajit,
I have got a simple working example here for you to try : I did this on 7.1.8.
I hope this helps get you started.
Cheers
John
NOTES:
I found this online REST (uses JSON) testing site: http://jsonplaceholder.typicode.com/
I just used to generate a simple JSON string:
http://jsonplaceholder.typicode.com/posts/1
{ "userId": 1, "id": 1, "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto" }
And from the link that Rajiv posted here:
https://pdn.pega.com/forums/functions/convert-clipboard-page-andor-xml-to-json
There is this reply:
//
Hi Subhajit,
I have got a simple working example here for you to try : I did this on 7.1.8.
I hope this helps get you started.
Cheers
John
NOTES:
I found this online REST (uses JSON) testing site: http://jsonplaceholder.typicode.com/
I just used to generate a simple JSON string:
http://jsonplaceholder.typicode.com/posts/1
{ "userId": 1, "id": 1, "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto" }
And from the link that Rajiv posted here:
https://pdn.pega.com/forums/functions/convert-clipboard-page-andor-xml-to-json
There is this reply:
//
The ClipboardPage java API has JSON and XML conversion methods - adoptJSONObject, adoptJSONArray, and adoptXMLForm for populating a clipboard page with JSON/XML data, getJSON and getXML for converting the contents of a clipboard page to an JSON/XML string value.
//
So it looks like we can use the 'adoptJSONObject' method here....
I built a 4 properties (just 'text') to mirror the JSON structure above 'userID', 'id', 'title' and 'body', and a simple Activity to create a Page and use a Java Step to call the 'adoptJSONObject' method:
Here's a screenshot of the Properties and the Activity Steps (I'll showt the JAVA step further down)
Here's the Parameters tab : for convenience I create a 'mirror' local variable of the parameter 'JSONString' - this just make the Java Step easier to write (we can refer directly to local variables, rather than having to fetch a parameter) - I transfer the PARAM to the LOCAL variable in the first step as you can see above.
And just a work page to that has the class of where my properties are:
The Java step is just this:
ClipboardPage stepPage = tools.getStepPage(); try { stepPage.adoptJSONObject( JSONString ); } catch(Exception e) { throw new PRRuntimeException(e); }
I'm not doing anything clever with Exception Handling as you can see, but this should be enough to at least log the Exception and Fail the Activity is something goes wrong.
Here's the running of the Activity:
I have just defaulted (on the Params tab itself for convenience) the JSONString to the output we generated earlier from http://jsonplaceholder.typicode.com/posts/1
Here's the resultant 'Show Page':
Here's the same XML in text format below:
<?xml version="1.0"?> <pagedata> <id>1</id> <body>quia et suscipit suscipit recusandae consequuntur expedita et cum reprehenderit molestiae ut ut quas totam nostrum rerum est autem sunt rem eveniet architecto</body> <title>sunt aut facere repellat provident occaecati excepturi optio reprehenderit</title> <userId>1</userId> <pzStatus>false</pzStatus> </pagedata>
Give it a shot on your JSON and see whether it works for you or not !
Cheers
John
Pegasystems
IN
This might be useful - https://pdn.pega.com/forums/functions/convert-clipboard-page-andor-xml-to-json
Pegasystems
IN
Alternatively, try Pega API - https://pdn.pega.com/integration/pega-api-for-the-pega-7-platform
Commonwealth Bank of Australia
AU
I tried to understand the Pega 7 API but could not understand much. Is there any tutorial which has a step by step description of JSON parsing
Pegasystems Inc.
US
One approach is to build a REST service and let PRPC automatically map the message data by selecting JSON in the Map to field. An example of how to do this can be found in the mesh post “Using JSON in a RESTFul CRUD Service” at the following URL: https://mesh.pega.com/message/185352#185352
For further details on the approach, also see the following:
“Securing a RESTFul Service with TLS SSL” at the following URL: https://mesh.pega.com/message/183331#183331
“Creating a RESTFul CRUD Service with Pega 7” at the following URL: https://mesh.pega.com/message/182369#182369
Commonwealth Bank of Australia
AU
Hi Francois,
I am unable to access the links you mentioned. It says unauthorized
Pegasystems Inc.
US
Unfortunately, it looks like those links are to internal spaces, so you will not be able to access them.
However, as far a Pega API tutorials go, I'm not sure if this does the trick, but here's a great document that was contributed to this community about getting up and running with the Pega API.
Commonwealth Bank of Australia
AU
Thanks Brendan for the document
Pegasystems Inc.
US
Hi Subhajit,
I could probably send you the packages directly.
Hi Francois,
You can probably upload the document to the Community documents.
-Adi
Pegasystems Inc.
US
Just keeping in mind that any content posted to this space is viewable by customers and partners. If it is appropriate to be viewed by that audience, please, by all means, post the content here for the entire communities' benefit.
If you need help posting/uploading any content, let me know outside of this discussion and I can help you out.
Thanks.
B.
PS. I am out tomorrow, so may have to wait until Monday depending on when/if you get in touch.
Commonwealth Bank of Australia
AU
Sure Francois.
That shall be extremely helpful
Accepted Solution
Pegasystems Inc.
GB
Hi Subhajit,
I have got a simple working example here for you to try : I did this on 7.1.8.
I hope this helps get you started.
Cheers
John
NOTES:
I found this online REST (uses JSON) testing site: http://jsonplaceholder.typicode.com/
I just used to generate a simple JSON string:
http://jsonplaceholder.typicode.com/posts/1
{ "userId": 1, "id": 1, "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto" }
And from the link that Rajiv posted here:
https://pdn.pega.com/forums/functions/convert-clipboard-page-andor-xml-to-json
There is this reply:
//
Hi Subhajit,
I have got a simple working example here for you to try : I did this on 7.1.8.
I hope this helps get you started.
Cheers
John
NOTES:
I found this online REST (uses JSON) testing site: http://jsonplaceholder.typicode.com/
I just used to generate a simple JSON string:
http://jsonplaceholder.typicode.com/posts/1
{ "userId": 1, "id": 1, "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto" }
And from the link that Rajiv posted here:
https://pdn.pega.com/forums/functions/convert-clipboard-page-andor-xml-to-json
There is this reply:
//
The ClipboardPage java API has JSON and XML conversion methods - adoptJSONObject, adoptJSONArray, and adoptXMLForm for populating a clipboard page with JSON/XML data, getJSON and getXML for converting the contents of a clipboard page to an JSON/XML string value.
//
So it looks like we can use the 'adoptJSONObject' method here....
I built a 4 properties (just 'text') to mirror the JSON structure above 'userID', 'id', 'title' and 'body', and a simple Activity to create a Page and use a Java Step to call the 'adoptJSONObject' method:
Here's a screenshot of the Properties and the Activity Steps (I'll showt the JAVA step further down)
Here's the Parameters tab : for convenience I create a 'mirror' local variable of the parameter 'JSONString' - this just make the Java Step easier to write (we can refer directly to local variables, rather than having to fetch a parameter) - I transfer the PARAM to the LOCAL variable in the first step as you can see above.
And just a work page to that has the class of where my properties are:
The Java step is just this:
ClipboardPage stepPage = tools.getStepPage(); try { stepPage.adoptJSONObject( JSONString ); } catch(Exception e) { throw new PRRuntimeException(e); }
I'm not doing anything clever with Exception Handling as you can see, but this should be enough to at least log the Exception and Fail the Activity is something goes wrong.
Here's the running of the Activity:
I have just defaulted (on the Params tab itself for convenience) the JSONString to the output we generated earlier from http://jsonplaceholder.typicode.com/posts/1
Here's the resultant 'Show Page':
Here's the same XML in text format below:
<?xml version="1.0"?> <pagedata> <id>1</id> <body>quia et suscipit suscipit recusandae consequuntur expedita et cum reprehenderit molestiae ut ut quas totam nostrum rerum est autem sunt rem eveniet architecto</body> <title>sunt aut facere repellat provident occaecati excepturi optio reprehenderit</title> <userId>1</userId> <pzStatus>false</pzStatus> </pagedata>
Give it a shot on your JSON and see whether it works for you or not !
Cheers
John
-
Divya Potti
Commonwealth Bank of Australia
AU
Thank you very much for the instructions that you provided.
We were able to get the parsed data in the clipboard but the pages were classless.
One of my friend cum colleague helped me to convert the pages into XML and then use Apply-Parse-XML to parse the data into properties.
Pegasystems Inc.
GB
Great - glad you got it working - any chance you post the code snippets you used - might be useful for other people to see....
Also : I hadn't noticed that in my example the page (even though it was originally created with a PRPC Class) created on my test was also Classless.....
I'm not sure that is correct behaviour actuallly - I was kinda hoping (but didn't notice) that the Class would be preserved on the page......