Sun May 19, 2019 9:24 pm
Login Register Lost Password? Contact Us


Reading a MultiLevel JSON into Thor

Questions around writing code and queries

Tue Jun 14, 2016 10:27 pm Change Time Zone

I have a JSON string like this:
{
account :
{
name : “string”,
Id : int,
legacy : { base : “string”, suffix : “string”},
status : “string”,
},
product : “Don1”,
productConfig :
{
generalConfig :
{
ConfigId : int,
Id2 : int,
ProdStatus : “string”,
},
email_notification:
{
EmailAddress : “string”,
sendFromEmailAddress : “string”
}
}
}
In order to work toward the above I put together a simpler string to try to get it to work:
Code: Select all
namesRec := RECORD
    UNSIGNED2 EmployeeID{xpath('EmpID')};
    STRING10 Firstname{xpath('FName')};
    STRING10 Lastname{xpath('LName')};
END;

EmpRec := RECORD
    DATASET(namesRec) AccountInfo{xpath('EmpRec')};
END;      
x := '{"EmpRec":"{""EmpID": 42,"FName": "George" , "LName": "Jetson" "}" }';

rec := FROMJSON(namesRec,x);
OUTPUT(rec);


The error I get is this:
Error: System error: 2: Error - syntax error "expected ',' or '}'" [file offset 14]
{"EmpRec":"{""*ERROR*EmpID": 42,"FName": "George" , "LName": "Jetson" "}" }

I have tried all sorts of ways to get this to work but I am stumped. Maybe I need to Parse the string first? Please assist.
georgeb2d
 
Posts: 93
Joined: Wed Dec 24, 2014 3:36 pm

Wed Jun 15, 2016 10:07 am Change Time Zone

georgeb2d,

I have also been unable to make this work. It appears that TOJSON and FROMJSON don't currently handle nested child datasets. You should submit a JIRA ticket for this.

The only workaround I can think of would be using PARSE to extract the data values from the JSON string.

HTH,

Richard
rtaylor
Community Advisory Board Member
Community Advisory Board Member
 
Posts: 1452
Joined: Wed Oct 26, 2011 7:40 pm

Wed Jun 15, 2016 12:20 pm Change Time Zone

Thanks. How do I submit a JIRA ticket?
georgeb2d
 
Posts: 93
Joined: Wed Dec 24, 2014 3:36 pm

Wed Jun 15, 2016 1:23 pm Change Time Zone

You can submit a ticket here http://track.hpccsystems.com
Gleb Aronsky
 
Posts: 22
Joined: Fri Feb 10, 2012 1:49 pm

Wed Jun 15, 2016 1:49 pm Change Time Zone

Hold on, I spoke too soon. :D

I made two changes to your code and now it works:
Code: Select all
namesRec := RECORD
    UNSIGNED2 EmployeeID{xpath('EmpID')};
    STRING10 Firstname{xpath('FName')};
    STRING10 Lastname{xpath('LName')};
END;

EmpRec := RECORD
    DATASET(namesRec) AccountInfo{xpath('EmpRec')};
END;     
x := '{"EmpRec": {"EmpID": 42,"FName": "George" , "LName": "Jetson" } }';

rec := FROMJSON(EmpRec,x);
OUTPUT(rec);
I deleted the double quotes you had around your nested curly braces, and I changed the RECORD structure name in the FROMJSON function to EmpRec. It works correctly now -- no JIRA needed.

HTH,

Richard
rtaylor
Community Advisory Board Member
Community Advisory Board Member
 
Posts: 1452
Joined: Wed Oct 26, 2011 7:40 pm

Wed Jun 15, 2016 5:38 pm Change Time Zone

Thanks. Works great.
georgeb2d
 
Posts: 93
Joined: Wed Dec 24, 2014 3:36 pm

Wed May 08, 2019 4:01 pm Change Time Zone

How would you handle a list of items like in dataset 'y' for 'familyNames'?

Code: Select all
namesRec := RECORD
    UNSIGNED2 EmployeeID{xpath('EmpID')};
    STRING10 Firstname{xpath('FName')};
    STRING10 Lastname{xpath('LName')};
END;

EmpRec := RECORD
    DATASET(namesRec) AccountInfo{xpath('EmpRec')};
END;     
x := '{"EmpRec": {"EmpID": 42,"FName": "George" , "LName": "Jetson" }}';
y := '{"EmpRec": {"EmpID": 42,"FName": "George" , "LName": "Jetson" }, "familyNames": ["Oh George","Jetson My Boy" ]}';

rec := FROMJSON(EmpRec,x);
OUTPUT(rec);
wjblack
 
Posts: 25
Joined: Mon Jul 11, 2016 12:45 pm

Wed May 08, 2019 4:11 pm Change Time Zone

Added SET OF STRING familyNames{xpath('familyNames')};

Code: Select all
namesRec := RECORD
    UNSIGNED2 EmployeeID{xpath('EmpID')};
    STRING10 Firstname{xpath('FName')};
    STRING10 Lastname{xpath('LName')};
END;

EmpRec := RECORD
    DATASET(namesRec) AccountInfo{xpath('EmpRec')};
    SET OF STRING familyNames{xpath('familyNames')};
END;     
x := '{"EmpRec": {"EmpID": 42,"FName": "George" , "LName": "Jetson" }}';
y := '{"EmpRec": {"EmpID": 42,"FName": "George" , "LName": "Jetson" }, "familyNames": ["Oh George","Jetson My Boy" ]}';

rec := FROMJSON(EmpRec,y);
OUTPUT(rec);
wjblack
 
Posts: 25
Joined: Mon Jul 11, 2016 12:45 pm


Return to Programming

Who is online

Users browsing this forum: Bing [Bot] and 0 guests

cron