json parsing

Posted by jmls on 25-Aug-2013 02:11

Looking for a cross-platform solution (so can't use .net or a wrapper around .net)

given this json string

{"contact":{"first_name":"j","last_name":"smth","email":null,"notes":null,"phone_numbers":[]}}

what would I need to do in order to create a contact record and related phone_number(s) ?

I've tried (and failed) with the json parser in 11.3 (but that might be because of the appalling documentation)

I've tried with temp-tables and datasets (but get a parsing error (unexpected braces)

looking for some inspiration on this damp and dreary day

All Replies

Posted by Stefan Drissen on 25-Aug-2013 03:56

DEFINE TEMP-TABLE contact
   FIELD FIRST_name AS CHAR
   FIELD LAST_name AS CHAR
   FIELD email AS CHAR
   FIELD notes AS CHAR
   FIELD phone_numbers AS CHAR EXTENT 5
   .


DEF VAR lcc AS LONGCHAR .
   
lcc = '~{"contact":~{"first_name":"j","last_name":"smth","email":null,"notes":null,"phone_numbers":[]}}'.

TEMP-TABLE contact:DEFAULT-BUFFER-HANDLE:READ-JSON( "longchar", lcc ).

Posted by jmls on 25-Aug-2013 12:32

the problem with giving simple examples is that you get the correct answer, but then there's always a "but"

there is a possibility that the embedded json (phone_number) may actually be another object (think of orders and order lines)

I tried to use a prodataset (order/orderline) and got the same issue

I should also say this is 11.3

thanks for the hint, though !

Posted by Admin on 25-Aug-2013 12:35

        DEFINE VARIABLE oJsonObject AS JsonObject NO-UNDO .

        DEFINE VARIABLE oObjectModel AS ObjectModelParser NO-UNDO .

        oObjectModel = NEW ObjectModelParser () .

        oJsonObject = CAST (oObjectModel:Parse (), JsonObject).

Posted by jmls on 25-Aug-2013 14:34

Thanks Mike - this may work well for me.

This thread is closed