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
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
Edgar
Two options:
- 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)
- 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
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
Wil
Harm's code allows you to chose what to remove - empty strings and/or zero numbers and/or null values:
So just call:Code:Procedure RemoveEmptyMembers Handle hoJsonObj Boolean bEmptyString Boolean bZeroNumber Boolean bNull
MikeCode:Send RemoveEmptyMembers False False True
Yep, that makes perfect sense. Thanks Mike.
I also updated the wiki article to include your variant of removing the nulls.
--
Wil
Hi All
I tried this procedure and it has worked in all tests even with somewhat complex structures, we will continue testing.
thanks guysCode: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
Edgar
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
thanks Focus
you're right, I'll correct
Edgar
Edgar
If you are in DF 19.1, how about this instead:
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).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"
Mike
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