Search code examples
jsonstructuniface

How to create a list with struct for a json in Uniface


i need to create a json with the programming language uniface. I use a struct to create the json. But i have a problem to create a list with a struct.

See here my example for the list "Children".

Example - JSON:

{
    "First Name" : "Barbara",
    "Last Name" :  "Singh",
    "Date of birth" :   { "year" : 1955, "month" : 1, "day" : 23 }, 
    "Married" : true,
    "Children" :  
    [
        {  "Name" : "Martin", "Year of birth" :  1980 },
        {  "Name" : "Margaret", "Year of birth" : 1983 }        
    ],
    "Mobile phone" : null
} 

Example - Struct:

[]
    [First Name] = "Barbara"
    [Last Name]  = "Singh"
    [Date of birth]
        [year] = 1732             
        [month] = 2               
        [day] = 22                
    [Married] = "T"               
    [Children]
        []
             [Name] = "Martin"
             [Year of birth] = 1980      
        []
             [Name] = "Margaret"   
             [Year of birth] = 1983      
     [Mobile phone] 

Example based on the Uniface documentation: Structs for JSON Data

So, how can i create a list with uniface for a json?


Solution

  • I found an solution based on the post "How to create a list with struct for a json in Uniface, which inspired my solution.

    Solution:

    myStructure = $newstruct
    myStructure->Children = $newstruct
    myStructure->Children->$tags->jsonClass="array"
    
    forlist sRecordPerson, nCounter in sListOfPersons
      sStruktur->Children-><NULL>{nCounter} = $newstruct
    
      sStruktur->Children-><NULL>{nCounter}->Name = $item("Name", sRecordPerson)
      sStruktur->Children-><NULL>{nCounter}->Yearofbirth = $item("Yearofbirth", sRecordPerson)
    
    endfor