Deserializing JSON to an Object

Posted by cgladue on 03-Jun-2013 16:38

Im new to Progress OO Programming but familier with C# and the .NET side of things...  I Have a JSON String that gets submitted to Webspeed and want to parse it into an Object/Dataset/Temptable or whatever you reccomend is the best way.  the string would look similar to the following

{"requests":

    [

        {"name": "getProfile1"},

        {"name": "getProfile2"},

        {"name": "getProfile3"},

    ],

    "cardNum": "00109926",

    "pin": "1234"

}

i cannot for the life of me figure out the correect schema to build (temp table/ dataset / etc) to get this to de-serialize correctly. 

Would anyone have any suggestions on the schema to build and the commend to deserialize correctly ?

thank you !!

All Replies

Posted by twc on 03-Jun-2013 18:45

Chad,

Summary:  ABL requires an array of objects.  Translation:  Frequently the incoming JSON format must be modified. 

With the correct JSON format and correct temp-table defined, it is as simple as:

     temp-table tRequest:read-json("FILE","request.json").  /* assuming file request.json holds the json. */

But, you already know that (posted for others).  To generate a json file the ABL will handle "as is", start out with the temp-table you want, or you think you want, add a few records to it and then run:

     temp-table tRequest:write-json("FILE","request.json",yes).

Compare the differences between your incoming format and the ABL output.  (BTW, the string posted does not parse correctly at a site I find very helpful:  http://json.parser.online.fr/ ). 

ABL expects json to be formatted in a certain fashion.  Check out the manual "Working with JSON" http://documentation.progress.com/output/OpenEdge112/pdfs/dvjsn/dvjsn.pdf (v11.2 manual since your version was not posted) and understand the difference between an "arrays" and "objects".  Arrays can hold objects and vise-versa.  An ABL temp-table expects an array of objects.  The array name is the temp-table and each object is a temp-table row.

If you want your temp-table/fields to have different names than the json, see "serialize-name" attribute (on both temp-table and temp-table fields).

Hope this gets you started in the right direction.

Tim

Sample Sports2000 code:

define temp-table tDepartment no-undo like Department.


for each Department no-lock:

   create tDepartment.

   buffer-copy Department to tDepartment.

end.

   /* optional 3rd parameter, formatted, is only set to yes

      for illustration - in production, don't use or set to no

      to keep the json "lighter weight" */

temp-table tDepartment:write-json("FILE","Department.json",yes).

Display file Department.json:

{"tDepartment": [

  {

    "DeptCode": "100",

    "DeptName": "Consulting"

  },

  {

    "DeptCode": "200",

    "DeptName": "Administration"

  },

  {

    "DeptCode": "300",

    "DeptName": "Marketing"

  },

  {

    "DeptCode": "400",

    "DeptName": "Sales"

  },

  {

    "DeptCode": "500",

    "DeptName": "Training"

  },

  {

    "DeptCode": "600",

    "DeptName": "Development"

  },

  {

    "DeptCode": "700",

    "DeptName": "Finance"

  }

]}

This thread is closed