Uniface User Forum

 View Only
  • 1.  jsonToStruct with null values

    PARTNER
    Posted 12-22-2021 05:08
    Hello everybody,

    i like to read an json from a web api.

    The following json show my example for a json for a car object:
    {
        "brand": "Audi",
        "model": null,
        "generation": null,
        "yearOfProduction": "1991"
    }​
    If i got the json, the values of 'model' and 'generation' are null.

    But if I transform the json to a struct with "jsonToStruct" method
    jsonToStruct sCarAsStruct, sCarAsJson​

    the values "model" and "generation" are empty strings

    []
      [brand] = "Audi"
      [model] = ""
      [generation] = ""
      [yearOfProduction] = "1991"
    


    My question: Is it possible to get values "model" and "generation" from the struct object as null values?

    Info: I like to save the car object in my database and want to difference between an empty string and a null value.



    ------------------------------
    Viktor Kuenstler
    Abrechnungszentrum Emmendingen
    Emmendingen DE
    ------------------------------


  • 2.  RE: jsonToStruct with null values

    PARTNER
    Posted 12-22-2021 05:22
    Hi  Viktor,

    You can test the jsonDataType of the value "model" :
    struct->model->$tags->jsonDataType  return null .

    you can see  more struct info using struct->$dbgstring:

    []
    [$tags]
    [jsonClass] = object
    [brand] = "Audi"
    [$tags]
    [jsonDataType] = string
    [model] = ""
    [$tags]
    [jsonDataType] = null
    [generation] = ""
    [$tags]
    [jsonDataType] = null
    [yearOfProduction] = "1991"
    [$tags]
    [jsonDataType] = string


    Gilles

    ------------------------------
    Hortion Gilles
    Agfa-Gevaert NV
    ------------------------------



  • 3.  RE: jsonToStruct with null values

    PARTNER
    Posted 12-22-2021 05:28
    Thank you Gilles, thank you Daniel :)

    Best Regards

    ------------------------------
    Viktor Kuenstler
    Abrechnungszentrum Emmendingen
    Emmendingen DE
    ------------------------------



  • 4.  RE: jsonToStruct with null values

    ROCKETEER
    Posted 12-22-2021 05:24
    Hello Viktor,

    You need to check the jsonDataType tag of the "model" and "generation" node. This will return null.

    Here's the output of the generated struct using $dbgstring (e.g. putmess sCarAsStruct->$dbgstring):

    []
      [$tags]
        [jsonClass] = object
      [brand] = "Audi"
        [$tags]
          [jsonDataType] = string
      [model] = ""
        [$tags]
          [jsonDataType] = null
      [generation] = ""
        [$tags]
          [jsonDataType] = null
      [yearOfProduction] = "1991"
        [$tags]
          [jsonDataType] = string
    
    Here's an example of how to check if the value is null:

    if (sCarAsStruct->model->$tags->jsonDataType = "null")
    message/info "The value of the node model is null"
    endif

    I hope this helps.

    Kind Regards,

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



  • 5.  RE: jsonToStruct with null values

    PARTNER
    Posted 12-24-2021 09:32
    The problem with round tripping (json->struct->json) is that you can loose metadata information.  I explain this using an xml to json and visa versa example; The example xml->json:  json has only name-value pairs, so every xml node (e.g. attribute, element, processing-instruction, comment) becomes an name-value pair in json. so transforming json back to xml, can be arranged by prefix the name with a _ for example for attribute, __for comment, ___for a processing-instruction and so on. The otherway arround json has arrays in xml there are no array, so transforming to xml you need to add an element arround it called for example array. The same using json to struct and visa versa, meta information is gone.  The solution is simple: add metadata. The question is here what is your single source of truth? modify that and translate. e.g.  in case json, change json before translating.

    ------------------------------
    Dino Seelig
    Tcco
    Drunen NL
    ------------------------------