Posted: 12 Jan 2020 17:17 EST Last activity: 15 Jan 2020 8:51 EST
Storing data in JSON
Our project has a requirement of fetching the data from different applications through robotic automation and display them in Pega UI.I’ve developed the automation which interacts with different applications and fetches data as string (Exp - Name, Age, DOB etc) and/or DataTable (Exp- multiple Account's details) format. Now I’ve to store these data to JSON file but I’m not sure how to achieve this using jsonUtil component.
Can I store all data in a single json or I need multiple json to store the string and table data in separate json?
I’m looking forward for guidance how to do this, any sample automation will be appreciated.
JSON is just a way to convert an "object" into a string so it can be passed around easily. Whatever accepts it will then "derserialize" the string back into its original object. The most common use in Pega Robotics is to convert a DataTable into a string to be passed around or the other way where you receive a string and deserialize it back to whatever type it is. You could store a serialized DataTable as a string inside a table column, so in that respect, you could use one JSON string to store multiple things. I like to use a Key-Value-Pair table to store random stuff where the key is the name, so you can access the "Address" by simply getting that record from the table.
Thank you very much for the sample automation. I can understand teh purpose of JSON now however I need further help to convert my automation output to JSON. How the JsonUtil can be used to serialize the automation output, please refer the attached screenshot. Thanks,
You could utilize the SerializeObjectMethod to convert your DataTable to a JSON string. I did that in the example. If you wanted to store dtContacts in the Key-Value-Pair table, then you would serialize dtContacts before you add it to the KVP table. You could then serialize that KVP table to provide a single JSON string that contains scalar values in addition to more complex types like DataTables.
I cannot really answer how you should do it for your application. If whatever you are sending the string to accepts it in a specific format, you'll need to provide that format. If they can accept it as a serialized JSON object, then I would store everything in a LookupTable and serialize that and provide it that way as that would be easier to use in an automation.
If some other application is providing you the string, then you'll need to know if it is a serialized object or if it is a common JSON string as shown in the website for W3schools. If it is the latter, then you can use the "GetValueFromJSON" method to get something out, or the "Set..." method to put something in.