Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Null- json

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2009
    Location
    Colombia
    Posts
    2,509

    Default Null- json

    Hi All

    I am reading a Json file in a structure, but fields whose value is NULL, generates error.

    I'm with DF19.0, any solution?

    Edgar

  2. #2
    Join Date
    Mar 2009
    Location
    Beech Hill - a village near Reading in the UK
    Posts
    2,812

    Default Re: Null- json

    Edgar

    Two options:
    1. Pre-Process the JSON object (after loading the data, but before moving to a struct) to remove any nulls (somewhere here Harm posted code to do it with a recursive function)
    2. Manually load the data from the JSON object into the struct (which you can guard with "If not (MemberJsonType(hoJson, memberName) = jsonTypeNull)" (which can be a bit tedious wit a complex structure)


    Mike

  3. #3

    Default Re: Null- json

    Edgar,

    Does this help?
    https://dataflex.wiki/index.php/JSON...lable_elements

    FWIW, it sounds like the solution from Harm is the one you need in your case. Perhaps you might have to tweak it a bit to only address the jsonTypeNull one.

    --
    Wil

  4. #4
    Join Date
    Mar 2009
    Location
    Beech Hill - a village near Reading in the UK
    Posts
    2,812

    Default Re: Null- json

    Wil

    Harm's code allows you to chose what to remove - empty strings and/or zero numbers and/or null values:
    Code:
       Procedure RemoveEmptyMembers Handle hoJsonObj Boolean bEmptyString Boolean bZeroNumber Boolean bNull
    So just call:
    Code:
        Send RemoveEmptyMembers False False True
    Mike

  5. #5

    Default Re: Null- json

    Yep, that makes perfect sense. Thanks Mike.
    I also updated the wiki article to include your variant of removing the nulls.

    --
    Wil

  6. #6
    Join Date
    Feb 2009
    Location
    Colombia
    Posts
    2,509

    Default Re: Null- json

    Hi All
    I tried this procedure and it has worked in all tests even with somewhat complex structures, we will continue testing.

    Code:
                                        Set_Argument_Size 60000
                                      
                                        
                                       Get Seq_New_Channel to iChn
                                       Direct_Input channel iChn sArchivo
                                       Readln sJson
                                       Close_Input
                                       Send Seq_Release_Channel iChn
    
    
                                          Get Create (RefClass(cJsonObject))           to hoJsClientes
                                          Set pbRequireAllMembers of hoJsPlans         to False
                                           Move (Replaces("object",sJson,"Object"))    to sJson
                                           Move (Replaces("address",sJson,"Address"))  to sJson
                                           Move (Replaces("null",sJson,'"0"'))           to sJson
                                           Get ParseString of hoJsClientes sJson            to bOk    
                                           If (bOk) Get JsonToDataType of hoJsClientes      to tWisClientes
                                           Send Destroy of hoJsClientes
    thanks guys

    Edgar

  7. #7
    Join Date
    Feb 2009
    Posts
    5,468

    Default Re: Null- json

    Just curious. Why are you setting the argument size to something smaller than the default ?
    Success consists of going from failure to failure without loss of enthusiasm - Winston Churchill

  8. #8
    Join Date
    Feb 2009
    Location
    Colombia
    Posts
    2,509

    Default Re: Null- json

    thanks Focus

    you're right, I'll correct

    Edgar

  9. #9
    Join Date
    Mar 2009
    Location
    Beech Hill - a village near Reading in the UK
    Posts
    2,812

    Default Re: Null- json

    Edgar

    If you are in DF 19.1, how about this instead:
    Code:
    // The file "MyData.json" - yours will be more complicated, but just for the test:
    {
        "object": "Stuff in 'object'",
        "address": null,
        "array": [5, 10, 15, null, 2, 4, 8, 16],
        "Id": 567,
        "moreStuff": "Foo bar baz"
    }
    
    // The DataFlex test program:
    Use Windows.pkg
    Use cApplication.pkg
    Use cJsonObject.pkg  // Holds the jsonTypeXxxx definitions
    
    Object oApplication is a cApplication
    End_Object
    
    Struct stYourDataType
        Integer Id
        { Name="object" }
        String  Object
        { Name="address" }
        String Address
        Integer[] array
        String moreStuff
    End_Struct
    
    //...
    
    // Version of Harm's procedure, just for nulls.
    Procedure RemoveNullMembers Handle hoJsonObj
        Integer iTo iMember iType
        Handle hoMember
        String sMemberName
        Boolean bRemove
        
        Get MemberCount of hoJsonObj to iTo
        Decrement iTo
    
        For iMember from 0 to iTo
            Move False to bRemove
            Get MemberByIndex of hoJsonObj iMember to hoMember
            Get JsonType of hoMember to iType
             
            If (iType = jsonTypeNull) ;
                    Move True to bRemove
            Else If (iType = jsonTypeObject or iType = jsonTypeArray) ;
                    Send RemoveNullMembers hoMember
             
            If bRemove Begin
    
                If (IsOfJsonType(hoJsonObj, jsonTypeObject)) Begin
                    Get MemberNameByIndex of hoJsonObj iMember to sMemberName
                    Send RemoveMember of hoJsonObj sMemberName
                    Decrement iMember
                    Decrement iTo
                End
                Else If (IsOfJsonType(hoJsonObj, jsonTypeArray)) Begin
                    Send RemoveMember of hoJsonObj iMember
                    Decrement iMember
                    Decrement iTo
                End
    
            End
             
            Send Destroy of hoMember
        Loop
    
    End_Procedure
    
    //...
    
    Procedure Test String sArchivo
        stYourDataType tWisClientes
        UChar[] ucaJson
        Integer iChn
        Boolean bOK
        Handle  hoJsClientes
        
        // The equivalent of the code you posted:
        Get Seq_New_Channel to iChn
        Direct_input channel iChn sArchivo
        Read_Block channel iChn ucaJson -1
        Close_Input channel iChn
        Send Seq_Release_Channel iChn
        
        Get Create (RefClass(cJsonObject)) to hoJsClientes
        Set pbRequireAllMembers of hoJsClientes to False
        Get ParseUTF8 of hoJsClientes ucaJson to bOK
        
        If bOK Begin
            Send RemoveNullMembers hoJsClientes
            Get JsonToDataType of hoJsClientes to tWisClientes
        End
    
        Send Destroy of hoJsClientes  // Put a breakpoint here to see what is in tWisClientes
    End_Procedure
    
    Send Test "MyData.json"
    No string size worries (and trust me: they do come up!), no nasty "Replaces" (a technique I used before we had the "Name" meta-tag, but no longer needed).

    Mike

  10. #10
    Join Date
    Feb 2009
    Location
    Colombia
    Posts
    2,509

    Default Re: Null- json

    hi Mike, thanks

    Yes, in DF19.1 these problems are better treated, but I am struggling with the migration DF19.0 to DF19.1 (I already did tests) for the 3.0 graphics library that is leading me.

    Edgar

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •