Uniface User Forum

 View Only
  • 1.  XmltoStruct with multi occurrences

    Posted 04-29-2022 05:30
      |   view attached
    Hallo,

    I want to transform a xml to a json structure using xmlToStruct and structToJson. The xml contains multiple occurrences of an entity. But I didn't use a /schema swith so all attributes are elements. Converting to json gives an invalid result because de struct doesn't have tags as 'array' and 'object'. With a /schema and a schemaList parameter xmlToStruct is probably able to add the required tags. Has anyone a working example of a schemaList parameter?
    See my example in the appendix. 

    Or is the solution different?

    Greets, Johan Aalenhuis

    ------------------------------
    Johan Aalenhuis
    Crv B.V.
    Arnhem NL
    ------------------------------

    Attachment(s)

    txt
    appendixXmlToStruct.txt   1 KB 1 version


  • 2.  RE: XmltoStruct with multi occurrences

    ROCKETEER
    Posted 04-29-2022 07:24
    Edited by Daniel Iseli 05-02-2022 02:46
    Hello,

    The rules for converting a Struct to JSON are explained here:

    > Uniface Reference > ProcScript Reference > ProcScript: Statements > structToJson
    > Scripting Application Behavior > Transforming Complex Data Using Structs > Structs for JSON Data

    As you can see JSON data is using different Struct annotations than XML data. It therefore is necessary to restructure the source Struct and add some JSON-specific tags, which will help the structToJson statement with the conversion process.

    And it's also a good idea to check $procReturnContext after (e.g.) executing structToJson. The statement will log additional info there in case it encounters non-fatal errors when transforming the Struct to JSON.

    The (expected) JSON result in the attachment seems a bit odd. When I verify it with a JSON viewer then it is flagged as being invalid.

    Anyways, when I add the jsonClass "object" tag to the "entname" nodes of the source Struct then jsonToStruct gives me the following output:

    {"entname":{"field1":"AM","field2":"Totaal"},"entname":{"field1":"FER","field2":"Totaal"}}


    And when I empty the name of the "entname" nodes then I get the following output (as an array):

    [{"field1":"AM","field2":"Totaal"},{"field1":"FER","field2":"Totaal"}]

    I hope this helps.

    ------------------------------
    Daniel Iseli
    Principal Technical Support Engineer
    Uniface Services
    Rocket Software, Switzerland
    ------------------------------