structToJson doesn't create array
Author: dennis.visschers@sogeti.nl (visschde)
Hi guys, I want to create a message reporting a visit (and potentially more visits in 1 message). The receiving end of this message has defined a API with a visits array that includes one or more visit nodes in JSON, so i made this code to report 1 person visiting:
visits = $newstruct visits->visits = $newstruct ; i want the root node to be "visits", so i seem to need to do this visits->visits->visit{1} = $newstruct visits->visits->visit{1}->guest = $newstruct visits->visits->visit{1}->guest->customer_number = "%%pers_id.PERSON%%%" ; the receiving API defines this as a string, but i have it as numeric visits->visits->visit{1}->guest->first_name = firstname.PERSON visits->visits->visit{1}->guest->surname = name.PERSON (...)
just for testing and fun i added the following line:
visits->visits->visit{2} = $newstruct
Running all of this code creates a struct that is formed the way i expect it to be, but after executing structToJson on that struct, i noticed that the second visit is just added as a new node at the same level as the first visit. I would have expected the JSON format to look something like this:
"visits" [ "visit": { "guest" : { "customer_number" = "1234567890", "first_name" : "Dennis", "last_name" : "Visschers" } } "visit" {} ]
Instead i got this:
"visits" { "visit": { "guest" : { "customer_number" = "1234567890", "first_name" : "Dennis", "last_name" : "Visschers" } } "visit" {} }
Is Uniface not capable of creating arrays in JSON? Have i found a bug? Or am i doing something wrong? Thanks for anything you can suggest! :Dennis
6 Comments
Local Administrator
You need to tell Uniface that you want to export array instead of named objects. Try something like this:
Author: sochaz (zdenek.socha@fullsys.cz)
Local Administrator
Hi Dennis, sochaz already provided part of the answer. The result of the suggest change would be:
The following should do the trick:
The result will be:
If you are not sure how the Struct has to look like for a desired outcome then you also could convert a JSON stream (that has the desired format) to a Struct (using jsonToStruct). From there you can simply reverse-engineer the structure of the Struct. Hope this helps. Kind regards, Daniel Uniface Technical Support
Author: diseli (daniel.iseli@uniface.com)
Local Administrator
Thank you for your replies sochaz and Daniel! I tried Daniel's code and got this at compile-time: visits->visits->*{1} = $newstruct error: 1000 - Syntax error (Statement not valid) Did you make a typo or so?
Author: visschde (dennis.visschers@sogeti.nl)
Local Administrator
Which Version of Uniface are you using? With Uniface 9.7.03.02 I have no Problems :
Author: Lauterbach (norbert.lauterbach@infraserv.com)
Local Administrator
My customer is using 9.6.06.02 (X602_0122_1) I'll make sure to tell them to upgrade to 9.7
by the way: if i omit the asterisk and just tell Uniface to take the first occurrence of the array like this, it compiles!:
Author: visschde (dennis.visschers@sogeti.nl)
Local Administrator
So now it works the way it's intended, but i'm running into the next issue: in te debugger i cannot alter any values in a struct I'm willing to open a new topic for this, but i get the feeling this is deliberate, even though it makes my work as a developer harder.
Author: visschde (dennis.visschers@sogeti.nl)