VBA-tools / VBA-JSON

JSON conversion and parsing for VBA
MIT License
1.74k stars 565 forks source link

Passing null #245

Closed wiryonolau closed 1 year ago

wiryonolau commented 1 year ago

Hi, How do I pass null value ? I need convertToJson result to be as as null instead of "null"

Dim payload As New Dictionary

' How to set id to null.
' vbNullString, vbNull become 1 instead of null
' vbEmpty become 0
payload.Add "id", "null"
payload.Add "provider", "something"
payload.Add "name", "someone"

JsonConverter.ConvertToJson(payload)
Nick-vanGemeren commented 1 year ago

vbNullStringis a string constant of zero length, a but more efficient than its equivalent literal"". vbNull is a constant = 1 returned byVarType() when the variable has no valid data. You can use it on the receiving (ParseJson) side in code like If VarType(jsonObject("error")) <> vbNull Then

To send a null value , simply use Null directly or via a Variant.

Sub Test()
Dim payload As New Dictionary
Dim nullvar As Variant: nullvar = Null
payload.Add "id", nullvar
payload.Add "name", "someone"
Debug.Print JsonConverter.ConvertToJson(payload)
' =>{"id":null,"name":"someone"}
End Sub

If this solves your problem, please close your issue here.

wiryonolau commented 1 year ago

Ok it's working. Thanks.