Access/VBA Library

RecordSet2JSON

Returns a String containing a JSON representation of all records in a Recordset.

Syntax

RecordSet2JSON( rs )
Parameters
Name Type Description
rs Recordset Required. An open Recordset. Each record of this Recordset will be read.
Return value

String : JSON representation of all records in the Recordset . It will be an Array and each record in the Array will be represented as an object.

Code
Function RecordSet2JSON(rs As Recordset) As String
    
    Dim js As String
    Dim D As Dictionary
    Dim pfx As String
    js = "["
    If rs.RecordCount <> 0 Then
        rs.MoveFirst
        While Not rs.EOF
            Set D = FetchRow(rs)
            Concat js, pfx, DictionaryToJSON(D)
            pfx = ","
            rs.MoveNext
        Wend
    End If
    Concat js, "]"
    
    RecordSet2JSON = js
End Function

JSONtag:JSONDatabasetag:Database

Remarks

  • The JSON layout of a Collection is the same as for an Array.

See also

  • DictionaryToJSON
  • Concat
  • FetchRow