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

  1. You need to tell Uniface that you want to export array instead of named objects. Try something like this:

    visits->visits->$tags->jsonClass = "array"

    Author: sochaz (zdenek.socha@fullsys.cz)
  2. Hi Dennis, sochaz already provided part of the answer. The result of the suggest change would be:

    {
        "visits": [{
            "guest": {
                "customer_number": "1234567890",
                "first_name": "Dennis",
                "surname": "Visschers"
            }
        }]
    }

       The following should do the trick:

    visits = $newstruct
    visits->visits = $newstruct ; i want the root node to be "visits", so i seem to need to do this
    visits->visits->*{1} = $newstruct ; an empty child node of "visits" will make it a JSO array
    visits->visits->*{1}->visit{1} = $newstruct
    visits->visits->*{1}->visit{1}->guest = $newstruct
    visits->visits->*{1}->visit{1}->guest->customer_number = "%%pers_id.PERSON%%%" ; the receiving API defines this as a string, but i have it as numeric
    visits->visits->*{1}->visit{1}->guest->first_name = firstname.PERSON
    visits->visits->*{1}->visit{1}->guest->surname = name.PERSON

       The result will be:

    {
      "visits": [{
        "visit": {
          "guest": {
            "customer_number": "",
            "first_name": "",
            "surname": ""
          }
        }
      }]
    }

      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)
  3. 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)
  4. Which Version of Uniface are you using?   With Uniface 9.7.03.02 I have no Problems :  

    variables
    
    string V_JSON
    
    struct root
    
    endvariables
    
     
    
    root = $newstruct
    
    root->visits = $newstruct ; i want the root node to be "visits", so i seem to need to do this
    
    root->visits->*{1} = $newstruct ; an empty child node of "visits" will make it a JSO array
    
    root->visits->*{1}->visit_11{1} = $newstruct
    
    root->visits->*{1}->visit_11{1}->guest = $newstruct
    
    root->visits->*{1}->visit_11{1}->guest->customer_number = "111" ; the receiving API defines this as a string, but i have it as numeric
    
    root->visits->*{1}->visit_11{1}->guest->first_name = "AAA"
    
    root->visits->*{1}->visit_11{1}->guest->surname = "BBB"
    
     
    
    root->visits->*{1}->visit_11{2} = $newstruct
    
     
    
    root->visits->*{2} = $newstruct ; an empty child node of "visits" will make it a JSO array
    
    root->visits->*{2}->visit_21{1} = $newstruct
    
     
    
    structtojson /whitespace V_JSON, root
    
    putmess V_JSON

    Author: Lauterbach (norbert.lauterbach@infraserv.com)
  5. My customer is using 9.6.06.02 (X602_0122_1)  I'll make sure to tell them to upgrade to 9.7 Laugh   by the way: if i omit the asterisk and just tell Uniface to take the first occurrence of the array like this, it compiles!: 

    visits->visits{1}->visit{1}->

    Author: visschde (dennis.visschers@sogeti.nl)
  6. 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.Frown


    Author: visschde (dennis.visschers@sogeti.nl)