Access/VBA Library


Private function to create a valid JSON "value".


JSONvalue( Value )
Name Type Description
Value Variant Required. Value to be converted to JSON format.
Return value

String : To JSON converted value.

Private Function JSONvalue(Value) As String
    Dim V As String
    Dim D As Dictionary
    Dim c As Collection
    Select Case VarType(Value)
        Case vbEmpty, vbNull:
            V = "null"
        Case vbLong, vbInteger, vbByte:
            V = CStr(Value)
        Case vbSingle, vbDouble, vbCurrency, vbDecimal:
            V = Replace(CStr(Value), ",", ".")
        Case vbString:
            V = Quote(StringToJSON(CStr(Value)), Chr(34))
        Case vbDate:
            V = Quote(Format(Value, "yyyy-mm-dd"), Chr(34))
        Case vbBoolean:
            V = IIf(Value = True, "True", "False")
        Case vbArray To vbArray + vbVariant:
            V = ArrayToJSON(Value)
        Case vbObject:
            If TypeOf Value Is Dictionary Then
                Set D = Value
                V = DictionaryToJSON(D)
            ElseIf TypeOf Value Is Collection Then
                Set c = Value
                V = CollectionToJSON(c)
                Err.Raise vbObjectError, , "JSONvalue: invalid object type"
            End If
        Case Else:
                Err.Raise vbObjectError, , "JSONValue: Unknown variable type : " & VarType(Value)
    End Select
    JSONvalue = V
End Function



  • An error will raise when value is an object and it is it's object type is not Dictionary or Collection An error will also raise when value is a user-defined type. In this case you should extend this function to convert this type to JSON.


Date Changes
2017-10-20 Use StringToJSON function to convert strings.

See also

  • DictionaryToJSON
  • CollectionToJSON
  • ArrayToJSON
  • StringToJSON