JDoc
JDoc class
Signature
Dim myJDoc As New JDoc
Summary
A JSON document: loads, holds, queries and writes one JSON tree.
Parameters
None
Remarks
JDoc is the entry point, created with New JDoc; its concepts follow MSXML's DOMDocument: LoadJson and ParseError as loadXML and parseError, Root as documentElement, SelectSingleNode and SelectNodes with JPath instead of XPath.
The query members (JPath, SelectSingleNode, SelectNodes, Exists and the getters) work on the root node; their rules are described once on `JNode`.
JSON is strict RFC 8259: no comments, no trailing commas, no single quotes. The nesting depth is limited to 512 levels, because the parser runs on the Access UI thread and shares its stack with VBA.
Created with New JDoc or CreateObject.
| Member | Kind | Summary |
|---|---|---|
| `LoadJson` | Method | Parses JSON text into the document. |
| `Load` | Method | Reads a JSON file into the document. |
| `Save` | Method | Writes the document to a UTF-8 file. |
| `Json` | Property | Returns the whole document as compact JSON text. |
| `ToJson` | Method | Returns the whole document as JSON text. |
| `Root` | Property | Gets or sets the root node of the document. |
| `ParseError` | Property | Returns the details of the last failed load. |
| `CreateElement` | Method | Creates an empty JSON object that is not yet attached to the tree. |
| `CreateArray` | Method | Creates an empty JSON array that is not yet attached to the tree. |
| `CreateValue` | Method | Creates a JSON value that is not yet attached to the tree. |
| `JPath` | Method | Applies `JNode.JPath` to the root node. |
| `SelectSingleNode` | Method | Applies `JNode.SelectSingleNode` to the root node. |
| `SelectNodes` | Method | Applies `JNode.SelectNodes` to the root node. |
| `Exists` | Method | Applies `JNode.Exists` to the root node. |
| `GetString` | Method | Applies `JNode.GetString` to the root node. |
| `GetLong` | Method | Applies `JNode.GetLong` to the root node. |
| `GetDouble` | Method | Applies `JNode.GetDouble` to the root node. |
| `GetCurrency` | Method | Applies `JNode.GetCurrency` to the root node. |
| `GetDecimal` | Method | Applies `JNode.GetDecimal` to the root node. |
| `GetDate` | Method | Applies `JNode.GetDate` to the root node. |
| `GetBoolean` | Method | Applies `JNode.GetBoolean` to the root node. |
| `GetValue` | Method | Applies `JNode.GetValue` to the root node. |
| `SetValue` | Method | Sets the value at a simple path and creates missing objects on the way. |
| `DateZone` | Property | Gets or sets how times with a zone are read by GetDate and DateValue. |
Code example
Dim doc As New JDoc
If Not doc.LoadJson(responseText) Then
MsgBox "Invalid JSON in line " & doc.ParseError.Line & ": " & doc.ParseError.Reason
Exit Sub
End If
Debug.Print doc.GetString("$.customer.name")
Inheritance
None
LoadJson method
Signature
Function LoadJson(ByVal Json As String) As Boolean
Summary
Parses JSON text into the document.
Parameters
| Name | Type | Optional | Default | Description |
|---|---|---|---|---|
Json | String | No | – | The JSON text. |
Return value: Boolean – True on success; False when the text is not valid JSON, with the details in ParseError.
Remarks
A leading U+FEFF (byte order mark) is ignored. As MSXML's loadXML, an invalid text does not raise an error: the document is left empty and ParseError describes the problem (line, column, line text, reason). Always check the return value: after a failed load only the getters without a default (GetString(path) …) raise the error of the load (jeParseError, or jeFileError after Load). Queries report nothing - SelectNodes returns an empty list, so a For Each over it does not run, SelectSingleNode and Root return Nothing, Exists returns False, Json returns an empty string - and getters with a default return the default.
Code example
None
Inheritance
Declared by JDoc.
Load method
Signature
Function Load(ByVal FileName As String) As Boolean
Summary
Reads a JSON file into the document.
Parameters
| Name | Type | Optional | Default | Description |
|---|---|---|---|---|
FileName | String | No | – | The path of the file. |
Return value: Boolean – True on success; False when the file cannot be read or is not valid JSON, with the details in ParseError.
Remarks
The file is read as UTF-8 unless a byte order mark selects UTF-16 or UTF-32 (see Values and limits). As LoadJson, Load raises no error: a file that cannot be read, or invalid content, empties the document and returns False; ParseError.ErrorCode is jeFileError or jeParseError. For jeFileError, Line, LinePos and FilePos are 0 and SrcText is empty.
Code example
None
Inheritance
Declared by JDoc.
Save method
Signature
Sub Save(ByVal FileName As String, Optional ByVal Indent As Boolean = True)
Summary
Writes the document to a UTF-8 file.
Parameters
| Name | Type | Optional | Default | Description |
|---|---|---|---|---|
FileName | String | No | – | The path of the file; an existing file is replaced. |
Indent | Boolean | Yes | True | True (default) for indented output; False for compact output. |
Remarks
A file that cannot be written raises jeFileError; the description names the cause (file or folder missing, path too long or invalid, access denied, otherwise the HRESULT). An empty document writes an empty file, which Load rejects.
Code example
None
Inheritance
Declared by JDoc.
Json property
Signature
Property Get Json() As String
Summary
Returns the whole document as compact JSON text.
Parameters
None
Property value: String – The JSON text; an empty string for an empty document.
Remarks
Read-only.
Code example
None
Inheritance
Declared by JDoc.
ToJson method
Signature
Function ToJson(Optional ByVal Indent As Boolean = False) As String
Summary
Returns the whole document as JSON text.
Parameters
| Name | Type | Optional | Default | Description |
|---|---|---|---|---|
Indent | Boolean | Yes | False | True for indented output with line breaks (CR LF); False for compact output. |
Return value: String – The JSON text.
Remarks
Output escapes " and \, the control characters U+0000-U+001F (\b \f \n \r \t, the others as \u00XX) and unpaired surrogates (\uXXXX); all other characters, including non-ASCII characters, /, U+007F, U+2028 and U+2029, are written as they are. Indented output uses two spaces and CR LF.
Code example
None
Inheritance
Declared by JDoc.
Root property
Signature
Property Get Root() As JNode Property Set Root(RHS As JNode)
Summary
Gets or sets the root node of the document.
Parameters
None
Property value: `JNode` – The root node; Nothing for an empty document.
Remarks
Assign a node created with CreateElement, CreateArray or CreateValue with Set doc.Root = node.
Read/write.
Code example
None
Inheritance
Declared by JDoc.
ParseError property
Signature
Property Get ParseError() As JParseError
Summary
Returns the details of the last failed load.
Parameters
None
Property value: `JParseError` – A `JParseError`; its ErrorCode is 0 after a successful load.
Remarks
Read-only.
Code example
None
Inheritance
Declared by JDoc.
CreateElement method
Signature
Function CreateElement() As JElement
Summary
Creates an empty JSON object that is not yet attached to the tree.
Parameters
None
Return value: `JElement` – The new `JElement`.
Remarks
None
Code example
None
Inheritance
Declared by JDoc.
CreateArray method
Signature
Function CreateArray() As JArray
Summary
Creates an empty JSON array that is not yet attached to the tree.
Parameters
Remarks
None
Code example
None
Inheritance
Declared by JDoc.
CreateValue method
Signature
Function CreateValue(ByVal Value As Variant) As JValue
Summary
Creates a JSON value that is not yet attached to the tree.
Parameters
| Name | Type | Optional | Default | Description |
|---|---|---|---|---|
Value | Variant | No | – | The value; the same types as `JNode.Value` accepts. |
Remarks
None
Code example
None
Inheritance
Declared by JDoc.
JPath method
Signature
Function JPath(ByVal Path As String) As JNode
Summary
Applies `JNode.JPath` to the root node.
Parameters
| Name | Type | Optional | Default | Description |
|---|---|---|---|---|
Path | String | No | – | A JPath expression. |
Return value: `JNode` – The first node found, or Nothing.
Remarks
None
Code example
None
Inheritance
Declared by JDoc.
SelectSingleNode method
Signature
Function SelectSingleNode(ByVal Path As String) As JNode
Summary
Applies `JNode.SelectSingleNode` to the root node.
Parameters
| Name | Type | Optional | Default | Description |
|---|---|---|---|---|
Path | String | No | – | A JPath expression. |
Return value: `JNode` – The first node found, or Nothing.
Remarks
None
Code example
None
Inheritance
Declared by JDoc.
SelectNodes method
Signature
Function SelectNodes(ByVal Path As String) As JNodeList
Summary
Applies `JNode.SelectNodes` to the root node.
Parameters
| Name | Type | Optional | Default | Description |
|---|---|---|---|---|
Path | String | No | – | A JPath expression. |
Return value: `JNodeList` – A `JNodeList` of the nodes found.
Remarks
None
Code example
None
Inheritance
Declared by JDoc.
Exists method
Signature
Function Exists(ByVal Path As String) As Boolean
Summary
Applies `JNode.Exists` to the root node.
Parameters
| Name | Type | Optional | Default | Description |
|---|---|---|---|---|
Path | String | No | – | A JPath expression. |
Return value: Boolean – True when at least one node is found.
Remarks
None
Code example
None
Inheritance
Declared by JDoc.
GetString method
Signature
Function GetString(ByVal Path As String, Optional ByVal Default As Variant) As String
Summary
Applies `JNode.GetString` to the root node.
Parameters
| Name | Type | Optional | Default | Description |
|---|---|---|---|---|
Path | String | No | – | A JPath expression. |
Default | Variant | Yes | – | The value returned when the path finds nothing or finds null. |
Return value: String – The value as String.
Remarks
None
Code example
None
Inheritance
Declared by JDoc.
GetLong method
Signature
Function GetLong(ByVal Path As String, Optional ByVal Default As Variant) As Long
Summary
Applies `JNode.GetLong` to the root node.
Parameters
| Name | Type | Optional | Default | Description |
|---|---|---|---|---|
Path | String | No | – | A JPath expression. |
Default | Variant | Yes | – | The value returned when the path finds nothing or finds null. |
Return value: Long – The value as Long.
Remarks
None
Code example
None
Inheritance
Declared by JDoc.
GetDouble method
Signature
Function GetDouble(ByVal Path As String, Optional ByVal Default As Variant) As Double
Summary
Applies `JNode.GetDouble` to the root node.
Parameters
| Name | Type | Optional | Default | Description |
|---|---|---|---|---|
Path | String | No | – | A JPath expression. |
Default | Variant | Yes | – | The value returned when the path finds nothing or finds null. |
Return value: Double – The value as Double.
Remarks
None
Code example
None
Inheritance
Declared by JDoc.
GetCurrency method
Signature
Function GetCurrency(ByVal Path As String, Optional ByVal Default As Variant) As Currency
Summary
Applies `JNode.GetCurrency` to the root node.
Parameters
| Name | Type | Optional | Default | Description |
|---|---|---|---|---|
Path | String | No | – | A JPath expression. |
Default | Variant | Yes | – | The value returned when the path finds nothing or finds null. |
Return value: Currency – The value as Currency.
Remarks
None
Code example
None
Inheritance
Declared by JDoc.
GetDecimal method
Signature
Function GetDecimal(ByVal Path As String, Optional ByVal Default As Variant) As Variant
Summary
Applies `JNode.GetDecimal` to the root node.
Parameters
| Name | Type | Optional | Default | Description |
|---|---|---|---|---|
Path | String | No | – | A JPath expression. |
Default | Variant | Yes | – | The value returned when the path finds nothing or finds null. |
Return value: Variant – The value as a Decimal Variant.
Remarks
None
Code example
None
Inheritance
Declared by JDoc.
GetDate method
Signature
Function GetDate(ByVal Path As String, Optional ByVal Default As Variant) As Date
Summary
Applies `JNode.GetDate` to the root node.
Parameters
| Name | Type | Optional | Default | Description |
|---|---|---|---|---|
Path | String | No | – | A JPath expression. |
Default | Variant | Yes | – | The value returned when the path finds nothing or finds null. |
Return value: Date – The value as Date.
Remarks
None
Code example
None
Inheritance
Declared by JDoc.
GetBoolean method
Signature
Function GetBoolean(ByVal Path As String, Optional ByVal Default As Variant) As Boolean
Summary
Applies `JNode.GetBoolean` to the root node.
Parameters
| Name | Type | Optional | Default | Description |
|---|---|---|---|---|
Path | String | No | – | A JPath expression. |
Default | Variant | Yes | – | The value returned when the path finds nothing or finds null. |
Return value: Boolean – The value as Boolean.
Remarks
None
Code example
None
Inheritance
Declared by JDoc.
GetValue method
Signature
Function GetValue(ByVal Path As String, Optional ByVal Default As Variant) As Variant
Summary
Applies `JNode.GetValue` to the root node.
Parameters
| Name | Type | Optional | Default | Description |
|---|---|---|---|---|
Path | String | No | – | A JPath expression. |
Default | Variant | Yes | – | The value returned when the path finds nothing or finds null. |
Return value: Variant – The value as Variant.
Remarks
None
Code example
None
Inheritance
Declared by JDoc.
SetValue method
Signature
Function SetValue(ByVal Path As String, ByVal Value As Variant) As JNode
Summary
Sets the value at a simple path and creates missing objects on the way.
Parameters
| Name | Type | Optional | Default | Description |
|---|---|---|---|---|
Path | String | No | – | A simple path of names and positions, such as $.address.city or $.lines[0]; filters and wildcards are not allowed. |
Value | Variant | No | – | The value, or a node to insert. |
Return value: `JNode` – The node that holds the value.
Remarks
Missing objects and arrays along the path are created (an object for a following name, an array for a following position); an empty document gets one as its root. A position equal to the length of an array appends; a negative position counts from the end; any other position outside the array raises jeIndexOutOfRange. The path $ replaces the root. A step into a value of the wrong kind (a name into an array, a position into an object or value) raises jeInvalidOperation. Inserting a node follows the rules of `JElement.SetValue`.
Code example
doc.SetValue "$.address.city", "Chicago" ' creates "address" when missing
Inheritance
Declared by JDoc.
DateZone property
Signature
Property Get DateZone() As JDateZone Property Let DateZone(RHS As JDateZone)
Summary
Gets or sets how times with a zone are read by GetDate and DateValue.
Parameters
None
Property value: `JDateZone` – One of the `JDateZone` constants; the default is jdzLocal.
Remarks
Writing a Date never adds a zone, because a VBA Date has none.
Read/write.
Code example
None
Inheritance
Declared by JDoc.