JSON and Nullable elements: Difference between revisions

Fixup text as things are never as clear cut as they appear to be.
(found yet another functions that deals with nulls)
(Fixup text as things are never as clear cut as they appear to be.)
Line 1: Line 1:
=== The nullable JSON issue ===
=== The nullable JSON issue ===
Using DataFlex it is very convenient to fill up your JSON data from a struct when using the [https://docs.dataaccess.com/dataflexhelp/mergedProjects/VDFClassRef/cJsonObject.htm cJSONOjbect] class. See for example our [[Create JSON from struct]] article.
Using DataFlex it is very convenient to fill up your JSON data from a struct when using the [https://docs.dataaccess.com/dataflexhelp/mergedProjects/VDFClassRef/cJsonObject.htm cJSONOjbect] class. See for example our [[Create JSON from struct]] article.


Quite often that [[JSON]] data is then send to a [[REST]] service and depending on that service they might not want to receive all the elements in your JSON in order to work correctly.
Quite often that [[JSON]] data is then send to a [[REST]] service and depending on that service they might not want to receive all the elements in your JSON in order to work correctly.


This has been debated before in the forum (1) a few times and currently the official way to do so is by traversing the JSON data structure and then removing the members that the other side does not want to receive using the [https://docs.dataaccess.com/dataflexhelp/mergedProjects/VDFClassRef/cJsonObject-Procedure-RemoveMember.htm RemoveMember] method.
There's also the issue of receiving JSON data with a null in there and not being able to process that automatically via a struct as DataFlex variables are not nullable.
 
This has been debated before in the forum (1) a few times and currently the official way to do so is by traversing the JSON data structure and then removing the members that the other side does not want to process by using the [https://docs.dataaccess.com/dataflexhelp/mergedProjects/VDFClassRef/cJsonObject-Procedure-RemoveMember.htm RemoveMember] method.


<source lang="dataflex">
<source lang="dataflex">
Line 79: Line 82:


Yes it's readable, No it's not so easy to support once you get multiple levels deep for your address element as you now have to make sure you destroy the objects etc..
Yes it's readable, No it's not so easy to support once you get multiple levels deep for your address element as you now have to make sure you destroy the objects etc..
=== The RemoveNamedMember method ===
So when looking at the line with the actual condition on when to remove a specific element :
<source lang="dataflex">
  If (data.addresses.shipto.latitude=0.0 and data.addresses.shipto.longitude=0.0) Begin
</source>
it looked like it would be good to have a method that could take care of this in one line as well.
eg. this would be nice to have:
<source lang="dataflex">
  If (data.addresses.shipto.latitude=0.0 and data.addresses.shipto.longitude=0.0) Begin
    Get RemoveNamedMember hoJsonRequest "addresses.shipTo.latitude"
    Get RemoveNamedMember hoJsonRequest "addresses.shipTo.longitude"
  End
</source>
it will then also work for a json structure where the address is a level deeper without having to write new code.
eg.
<source lang="dataflex">
    Get RemoveNamedMember hoJsonRequest "Returns.addresses.shipTo.longitude"
</source>
Turned out it was fairly easy to write (easier than writing this post!)
<source lang="dataflex">
 
  //
  // Used to strip a named/value pair from json
  // You can use this to directly remove a JSON member at a lower level from the JSON
  // object passed via hoJSON.
  // The member to be removed uses the exact JSON member names separated by dots.
  // Beware that JSON member names are case sensitive!
  //
  // If the member does not exist a runtime error will be triggered.
  // Eg.
  //  Send RemoveNamedMember hoJsonRequest "Order.addresses.shipTo.latitude"
  //
  Procedure RemoveNamedMember Handle hoJson String sName
    Integer iDotPos
    Integer eType
    Handle  hoChild
    String  sMember
   
    Move (Pos(".",sName)) to iDotPos
    If (iDotPos>0) Begin
      Move (Left(sName,iDotPos-1)) To sMember
      Move (Replace(sMember+".",sName,"")) To sName
      //Get HasMember of hoJson sMember to bHasMember <-- use this if you want to filter the runtime error (we currently do not)
      Get MemberJsonType of hoJson sMember to eType
      If (eType=jsonTypeObject) Begin
        Get Member of hoJson sMember to hoChild
        If (hoChild) Begin
          Send RemoveNamedMember hoChild sName
          Send Destroy Of hoChild
        End
      End
    End
    Else Begin
      Send RemoveMember Of hoJson sName
    End
  End_Procedure
</source>
Note that if you don't want the runtime error that you can use the HasMember test.
We don't use that as we always first convert from struct so we know the element exists. If the element is not there then there's likely a case sensitivity issue, so getting a runtime error helps when debugging your code.


=== Harm's RemoveEmptyMembers method ===
=== Harm's RemoveEmptyMembers method ===
Line 144: Line 215:


That works, but I'm personally a bit cautious about removing all elements that have value "0".  
That works, but I'm personally a bit cautious about removing all elements that have value "0".  
Sometimes we want pass that actual value, empty or zero does not equal null. There might be another element with the value 0 that should not be removed.
Sometimes we want pass that actual value, empty or zero does not equal null. There might be another element with the value 0 that should not be removed. However, sometimes it is exactly what you need.
 
Personally I would rather remove a specified element.


=== The RemoveNamedMember method ===
If you just want to remove the nulls before you move the data to a struct, then perhaps Mike Peat's variant of Harm's code is a bit more clear:


So when looking at the line with the actual condition on when to remove a specific element :
<source lang="dataflex">
<source lang="dataflex">
  If (data.addresses.shipto.latitude=0.0 and data.addresses.shipto.longitude=0.0) Begin
// Version of Harm's procedure, just for nulls.
</source>
Procedure RemoveNullMembers Handle hoJsonObj
it looked like it would be good to have a method that could take care of this in one line as well.
    Integer iTo iMember iType
    Handle hoMember
    String sMemberName
    Boolean bRemove
   
    Get MemberCount of hoJsonObj to iTo
    Decrement iTo


eg. this would be nice to have:
    For iMember from 0 to iTo
<source lang="dataflex">
        Move False to bRemove
  If (data.addresses.shipto.latitude=0.0 and data.addresses.shipto.longitude=0.0) Begin
        Get MemberByIndex of hoJsonObj iMember to hoMember
    Get RemoveNamedMember hoJsonRequest "addresses.shipTo.latitude"
        Get JsonType of hoMember to iType
    Get RemoveNamedMember hoJsonRequest "addresses.shipTo.longitude"
       
  End
        If (iType = jsonTypeNull) ;
</source>
                Move True to bRemove
        Else If (iType = jsonTypeObject or iType = jsonTypeArray) ;
                Send RemoveNullMembers hoMember
       
        If bRemove Begin


it will then also work for a json structure where the address is a level deeper without having to write new code.
            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


eg.
        End
<source lang="dataflex">
       
     Get RemoveNamedMember hoJsonRequest "Returns.addresses.shipTo.longitude"
        Send Destroy of hoMember
</source>
     Loop


Turned out it was fairly easy to write (easier than writing this post!)
End_Procedure
<source lang="dataflex">
 
  //
  // Used to strip a named/value pair from json
  // You can use this to directly remove a JSON member at a lower level from the JSON
  // object passed via hoJSON.
  // The member to be removed uses the exact JSON member names separated by dots.
  // Beware that JSON member names are case sensitive!
  //
  // If the member does not exist a runtime error will be triggered.
  // Eg.
  //  Send RemoveNamedMember hoJsonRequest "Order.addresses.shipTo.latitude"
  //
  Procedure RemoveNamedMember Handle hoJson String sName
    Integer iDotPos
    Integer eType
    Handle  hoChild
    String  sMember
   
    Move (Pos(".",sName)) to iDotPos
    If (iDotPos>0) Begin
      Move (Left(sName,iDotPos-1)) To sMember
      Move (Replace(sMember+".",sName,"")) To sName
      //Get HasMember of hoJson sMember to bHasMember <-- use this if you want to filter the runtime error (we currently do not)
      Get MemberJsonType of hoJson sMember to eType
      If (eType=jsonTypeObject) Begin
        Get Member of hoJson sMember to hoChild
        If (hoChild) Begin
          Send RemoveNamedMember hoChild sName
          Send Destroy Of hoChild
        End
      End
    End
    Else Begin
      Send RemoveMember Of hoJson sName
    End
  End_Procedure
</source>
</source>
 
See also (3)
Note that if you don't want the runtime error that you can use the HasMember test.
 
We don't use that as we always first convert from struct so we know the element exists. If the element is not there then there's likely a case sensitivity issue, so getting a runtime error helps when debugging your code.


=== External references ===
=== External references ===
Line 219: Line 267:
* (1) [https://support.dataaccess.com/Forums/showthread.php?64157-YAFR-DataTypeToJson-beyond-19-1 YAFR DataTypeToJson beyond 19.1] loong and mostly off topic debate at the forum about null support and JSON
* (1) [https://support.dataaccess.com/Forums/showthread.php?64157-YAFR-DataTypeToJson-beyond-19-1 YAFR DataTypeToJson beyond 19.1] loong and mostly off topic debate at the forum about null support and JSON
* (2) [https://support.dataaccess.com/Forums/showthread.php?62437-JsonToDataType&p=331876#post331876 JsonToDataType] Harm Wibier's solution
* (2) [https://support.dataaccess.com/Forums/showthread.php?62437-JsonToDataType&p=331876#post331876 JsonToDataType] Harm Wibier's solution
* (3) [https://support.dataaccess.com/Forums/showthread.php?64923-Null-json&p=348369#post348369 Json-Nulls] Mike Peat his variant
* [https://support.dataaccess.com/Forums/showthread.php?62358-Use-of-HasMember-of-cJSON-class-when-used-with-arrays&p=331630#post331630 RemoveNulls function by Mike Peat]
* [https://support.dataaccess.com/Forums/showthread.php?62358-Use-of-HasMember-of-cJSON-class-when-used-with-arrays&p=331630#post331630 RemoveNulls function by Mike Peat]


[[Category:JSON]]
[[Category:JSON]]