Platforms to show: All Mac Windows Linux Cross-Platform

Back to JSONMBS class.

Next items

JSONMBS.Add(Value as Variant)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 23.5 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Adds a value to an array.
Example
Var o As New JSONMBS

o.Add 1
o.Add "Hello"
o.Add JSONMBS.NewNullNode

MessageBox o.toString

Variant is converted to JSONMBS if needed.

If the self is an empty object, we replace it with an empty array and add the value.

Same as Add method.

JSONMBS.AddAt(index As Integer, value As Variant)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 23.5 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Insert a value to an array at the given index.
Example
Var o As New JSONMBS

o.Append 1
o.AddAt 1,2

MessageBox o.toString

o.AddAt 1,3

MessageBox o.toString

Variant is converted to JSONMBS if needed.

If the self is an empty object, we replace it with an empty array and insert the value.

Same as Insert method.

JSONMBS.AddItemToArray(item as JSONMBS)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 13.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Adds an item to an array.
Example
Var subscriptionId As Integer = 123456
Var Quantity As Integer = 1

// build inner object
Var ji As JSONMBS = JSONMBS.NewObjectNode

ji.AddItemToObject "subscriptionId", JSONMBS.NewNumberNode(subscriptionId)
ji.AddItemToObject "newQuantity", JSONMBS.NewNumberNode(Quantity)

// array around it
Var ja As JSONMBS = JSONMBS.NewArrayNode
ja.AddItemToArray ji

// and object around
Var jo As JSONMBS = JSONMBS.NewObjectNode
jo.AddItemToObject "subscriptionAmendmentParameters", ja


Var json As String = jo.toString
MessageBox json

'{
' "subscriptionAmendmentParameters": [
' {
' "subscriptionId": 123456,
' "newQuantity": 1
' }
' ]
'}

If you add item from new nodes created with plugin, we add them to the tree.
If you add items from existing node from other JSON tree, we add references.

Some examples using this method:

JSONMBS.AddItemToObject(label as string, value as JSONMBS)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 13.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Adds an item to an object with given label.
Example
Var subscriptionId As Integer = 123456
Var Quantity As Integer = 1

// build inner object
Var ji As JSONMBS = JSONMBS.NewObjectNode

ji.AddItemToObject "subscriptionId", JSONMBS.NewNumberNode(subscriptionId)
ji.AddItemToObject "newQuantity", JSONMBS.NewNumberNode(Quantity)

// array around it
Var ja As JSONMBS = JSONMBS.NewArrayNode
ja.AddItemToArray ji

// and object around
Var jo As JSONMBS = JSONMBS.NewObjectNode
jo.AddItemToObject "subscriptionAmendmentParameters", ja


Var json As String = jo.toString
MessageBox json

'{
' "subscriptionAmendmentParameters": [
' {
' "subscriptionId": 123456,
' "newQuantity": 1
' }
' ]
'}

If you add item from new nodes created with plugin, we add them to the tree.
If you add items from existing node from other JSON tree, we add references.

Some examples using this method:

JSONMBS.AddOrReplaceItemToObject(label as string, value as JSONMBS)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 22.5 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Adds an item to an object with given label.

Same as AddItemToObject, but removes old item for the label if it exists.

JSONMBS.Append(Value as Variant)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 23.5 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Appends a value to an array.
Example
Var o As New JSONMBS

o.Append 1
o.Append "Hello"
o.Append JSONMBS.NewNullNode

MessageBox o.toString

Variant is converted to JSONMBS if needed.

If the self is an empty object, we replace it with an empty array and add the value.

Same as Add method.

JSONMBS.ArrayItem(index as integer, Clone as Boolean = false) as JSONMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 13.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Queries array item with given index.

Added Clone parameter for version 20.4.
If Clone is true, we duplicate the JSON, so the new JSONMBS object does not point to origin JSONMBS object and exist independent.

Some examples using this method:

JSONMBS.ArrayItems(Clone as Boolean = false) as JSONMBS()

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 20.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Queries array items.

If Clone is true, we duplicate the JSON, so the new JSONMBS objects do not point to origin JSONMBS object and exist independent.

Some examples using this method:

JSONMBS.DeleteItem(index as Integer)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 19.1 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Deletes an item from an array/object by index.
Example
Var j As New JSONMBS

j.AddItemToObject "Hello", j.NewStringNode("Testing")
j.AddItemToObject "World", j.NewStringNode("Another node")

MsgBox j.toString

// delete named item
j.DeleteItem "Hello"

MsgBox j.toString

// delete first item
j.DeleteItem 0

MsgBox j.toString

Index is zero based.

See also:

JSONMBS.DeleteItem(label as string)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 19.1 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Deletes a named item from an object.
Example
Var j As New JSONMBS

j.AddItemToObject "Hello", j.NewStringNode("Testing")
j.AddItemToObject "World", j.NewStringNode("Another node")

MsgBox j.toString

// delete named item
j.DeleteItem "Hello"

MsgBox j.toString

// delete first item
j.DeleteItem 0

MsgBox j.toString

See also:

JSONMBS.Entries as JSONEntryMBS()

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 23.5 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Queries all entries.
Example
Var j As New JSONMBS

j.Value("Hello") = "World"
j.Value("Test") = "Value"
j.Value("abc") = "cde"

Var entries() As JSONEntryMBS = j.Entries

Break

Convenience function to get all entries.
Converts values to variants as needed.

Works for both objects and arrays.

JSONMBS.Equals(Other as JSONMBS) as Boolean

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 19.0 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Checks if two JSON are equal.
Example
Var j1 as JSONMBS = JSONMBS.NewNumberNode(5)
Var j2 as JSONMBS = JSONMBS.NewNumberNode(5)
Var j3 as JSONMBS = JSONMBS.NewNumberNode(6)

if not j1.Equals(j2) then
break // failed
end if

if j3.Equals(j2) then
break // failed
end if

Break // okay

We compare recursively all nodes.
If structure and values are the same, we return true, otherwise false.
Objects do not need to have same order for entries.

JSONMBS.FilterObjectArray(Name as String, Other as JSONMBS) as JSONMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 23.5 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Filters JSON object array to find matching entries.
Example
Var j As JSONMBS = JSONMBS.NewArrayNode

Var j1 As JSONMBS = JSONMBS.NewObjectNode
j1.AddItemToObject "id", JSONMBS.NewNumberNode(122)
j1.AddItemToObject "name", JSONMBS.NewStringNode("John")
j.AddItemToArray j1

Var j2 As JSONMBS = JSONMBS.NewObjectNode
j2.AddItemToObject "id", JSONMBS.NewNumberNode(123)
j2.AddItemToObject "name", JSONMBS.NewStringNode("Matt")
j.AddItemToArray j2

Var j3 As JSONMBS = JSONMBS.NewObjectNode
j3.AddItemToObject "id", JSONMBS.NewNumberNode(124)
j3.AddItemToObject "name", JSONMBS.NewStringNode("Bob")
j.AddItemToArray j3

Var resultArray As JSONMBS = j.FilterObjectArray("id", JSONMBS.NewNumberNode(123))
Var firstEntry As JSONMBS = resultArray.ArrayItem(0)

Break

Similar as if you loop and look for index of matching items with FindValueInObjectArray and transfer it yourself to a new array.

JSONMBS.FindValueInArray(Other as JSONMBS, StartIndex as Integer = 0) as Integer

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 23.5 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Queries index of value in array.
Example
Var j1 as JSONMBS = JSONMBS.NewIntegerArray(array(3,4,5,6,7))
Var j2 as JSONMBS = JSONMBS.NewNumberNode(5)
Var j3 as JSONMBS = JSONMBS.NewNumberNode(8)
Var j4 as JSONMBS = JSONMBS.NewStringNode("5")

Var index1 as integer = j1.FindValueInArray(j2) // we can find this
Var index2 as integer = j1.FindValueInArray(j3) // value not found
Var index3 as integer = j1.FindValueInArray(j4) // can't find string in number array

if index1 = 2 and index2 = -1 and index3 = -1 then
Break // okay
else
Break // failed
end if

Returns zero based index or -1 if not found.

Version 20.0 or newer allows with ByContent parameter = true to find by content, so number can be found via text.

StartIndex parameter added in version 21.5: Index of first element to check. Zero if not specified.
If you like to continue searching, you can pass last result + 1.

JSONMBS.FindValueInObjectArray(Name as String, Other as JSONMBS, StartIndex as Integer = 0) as Integer

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 23.5 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Queries index of value in object array.
Example
Var j1 as JSONMBS = JSONMBS.NewArrayNode
Var j2 as JSONMBS = JSONMBS.NewObjectNode

j2.AddItemToObject "Hello", JSONMBS.NewStringNode("World")
j2.AddItemToObject "ID", JSONMBS.NewStringNode("123")

j1.AddItemToArray j2

Var index1 as integer = j1.FindValueInObjectArray("ID", JSONMBS.NewStringNode("123")) // we can find this
Var index2 as integer = j1.FindValueInObjectArray("ID", JSONMBS.NewStringNode("456")) // value not found
Var index3 as integer = j1.FindValueInObjectArray("xxx", JSONMBS.NewStringNode("123")) // value not found

if index1 = 0 and index2 = -1 and index3 = -1 then
Break // okay
else
Break // failed
end if

Returns zero based index or -1 if not found.
We look into each object in the array, check if it has a value for the given label and compare that to the one to find.

Version 20.0 or newer allows with ByContent parameter = true to find by content, so number can be found via text.

StartIndex parameter added in version 21.5: Index of first element to check. Zero if not specified.
If you like to continue searching, you can pass last result + 1.

JSONMBS.HasChild(label as string) as Boolean

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 13.5 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Checks if a child node for the node with the given label exists.
Example
Var jv as JSONMBS = JSONMBS.NewStringNode("value")
Var jo as JSONMBS = JSONMBS.NewObjectNode

jo.AddItemToObject("key", jv)

// shows {"key": "value"}
MsgBox jo.toString
MsgBox str(jo.hasChild("key"))

Returns true if the entry exists or false if not.
Only for objects, not arrays.

Same as HasKey and HasName methods.

JSONMBS.HasKey(Key as string) as boolean

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 23.5 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Checks if a an object has the given key.
Example
Var jv as JSONMBS = JSONMBS.NewStringNode("value")
Var jo as JSONMBS = JSONMBS.NewObjectNode

jo.AddItemToObject("key", jv)

// shows {"key": "value"}
MsgBox jo.toString
MsgBox str(jo.HasKey("key"))

Returns true if the entry exists or false if not.
Only for objects, not arrays.

Same as HasChild and HasName methods.

JSONMBS.HasName(Name as string) as boolean

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 23.5 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Checks if a an object has the given key.
Example
Var jv as JSONMBS = JSONMBS.NewStringNode("value")
Var jo as JSONMBS = JSONMBS.NewObjectNode

jo.AddItemToObject("key", jv)

// shows {"key": "value"}
MsgBox jo.toString
MsgBox str(jo.HasName("key"))

Returns true if the entry exists or false if not.
Only for objects, not arrays.

Same as HasKey and HasChild methods.

Some examples using this method:

JSONMBS.Insert(index as integer, value as variant)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 23.5 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Adds a value into the index.
Example
Var o As New JSONMBS

o.Append 1
o.Insert 1,2

MessageBox o.toString

o.Insert 1,3

MessageBox o.toString

Variant is converted to JSONMBS if needed.

If the self is an empty object, we replace it with an empty array and insert the value.

Same as AddAt method.

JSONMBS.Iterate as JSONIteratorMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 23.5 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Iterate about child nodes.
Example
Var o As New JSONMBS

o.add 1
o.add 2
o.add 3

For Each v As JSONMBS In o.Iterate
Break
// watch in debugger
Next

Provides JSONMBS objects for inspection.

Warning: If you iterate while the JSONMBS is changed you may skip some entries or get duplicates, so please avoid editing it while iterating.

JSONMBS.IterateEntries as JSONIteratorMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 23.5 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Iterate about child entries.
Example
Var o As New JSONMBS

o.add 1
o.add 2
o.add 3

For Each v As JSONEntryMBS In o.IterateEntries
Break
// watch in debugger
Next

Entries are provided with JSONEntryMBS objects and include key and value.
For arrays, the key name is the index value.

Warning: If you iterate while the JSONMBS is changed you may skip some entries or get duplicates, so please avoid editing it while iterating.

JSONMBS.IterateValues as JSONIteratorMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 23.5 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Iterate about child values.
Example
Var o As New JSONMBS

o.add 1
o.add 2
o.add 3

For Each v As Variant In o.IterateValues
Break
// watch in debugger
// v has values like 1, 2 and 3
Next

The values are provided and converted to variant.

Warning: If you iterate while the JSONMBS is changed you may skip some entries or get duplicates, so please avoid editing it while iterating.

JSONMBS.KeyAt(index As Integer) as String

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 23.5 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Queries key name at given index.
Example
Var o As New JSONMBS

o.Value("Hello") = "World"

MessageBox o.KeyAt(0)

Raises an exception if index is out of range.

Same as Name or NameAt methods.
The order of keys can change if you add/remove values in the JSON object.

JSONMBS.Keys as String()

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 23.5 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Queries the key names for an object.
Example
Var o As New JSONMBS

o.Value("Hello") = "World"
o.Value("Test") = "Value"

Var Keys() As String = o.Keys
MessageBox string.FromArray(Keys)

The order is as stored in memory currently and that order changes if you edit the JSON object.
We cache the array so multiple calls would return the same array and you should not modify it.

Same as Names method.

Next items

The items on this page are in the following plugins: MBS Util Plugin.


The biggest plugin in space...