Monday, April 27, 2020

How to parse JSON which has spaces in its keys

In Pega, if we need to parse the array of objects from JSON, there are multiple ways to achieve the same. In this post, we would talk about how to parse the array of objects from JSON even when there are keys in the spaces.

Usually it is uncommon to have spaces within the keys of JSON. But, it is not mandatory that external systems would model in the ideal manner. So this brings a question on how to parse such JSON from Pega? Below is one of the approaches:

Step by Step Illustration:

1. In this example, we would pick a JSON with array of employee objects that needs to be parsed. Please note that keys in the below JSON has spaces within:













2. Create an activity  "ParseEmployeeDetails" with the below steps



  •     Set the parameters Param.jsonData to the above data by using escaped special characters
  •     Set Param.executionMode to DESERIALIZE
  •     The above two parameters would be passed to the data transform in next steps


3. Create a pagelist i.e. EmployeeList to hold the parsed array of object from JSON

4. Create a class "Data-Employee". Create properties within "Data-Employee" like ID, Name, Company, CompanyID to store the properties of each employee object.



5. Create Data Transform of type "JSON"
6. As shown below, we can choose to Top element structure to Array since it is is employee array of objects from JSON. And provide the Pagelist Property to which we should map to i.e. .EmployeeList.
In the below DT, we have two sides L.H.S = R.H.S
L.H.S here refers to properties within our pagelist.
R.H.S here refers to keys in the JSON










7. Run the activity ParseEmployeeDetails to see the output in tracer.










So we are able to parse the array of objects from JSON and store them on to pagelist(EmployeeList) in pega. Hope this helps. If there are any questions feel free to comment.

Thanks.

7 comments:

  1. What is the property set value used for ?

    ReplyDelete
    Replies
    1. Hi, Could you please elaborate your question. Property-Set is used to assign one Clipboard page Property to other clipboard page Property or a static value.

      Delete
  2. Hey Pavan, one quick question in Preperty set step when you are Set the parameters Param.jsonData parameters, are you setting for all 5 image shows one PAVN NAIDU, do we need to add rest other in separate steps or doesn't required Please let me know

    ReplyDelete
    Replies
    1. Hi, Good question. In the property-set I am setting like Param.jsonData to a string.

      That string is formed with the below steps:
      - Take the the entire JSON from notepad file.
      - Replace all " with \"
      - Brought all JSON data multiple lines to single line.
      - And enclosed this whole single line data into double quotes.
      - And I am setting this string to Param.jsonData on the whole.

      So no need to add anything else in the other steps.
      Hope this is clear. If not, let me know. Will be happy to help.

      Delete
  3. Thanks much Pavan for the reply.

    ReplyDelete
  4. do we really need a data type , why cant we use just use a pagelist with some pega properties ?

    ReplyDelete
    Replies
    1. In the usecase taken, data type is used. However your point is correct, we can directly map to pagelist too.

      Delete


HowToPega : All rights reserved and the contents are copyrighted to Pavan Kumar Naidu