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.Child(index As Integer) as JSONMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
property JavaScript Object Notation MBS Util Plugin 23.5 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Get or set child node.
Example
Var o As New JSONMBS

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

o.Child(1) = JSONMBS.NewNullNode

Var v As JSONMBS = o.Child(2) // see in debugger

MessageBox o.toString

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

Same as ChildAt method
Raises exception if index is out of range.
(Read and Write computed property)

See also:

JSONMBS.Child(Key As String) as JSONMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
property JavaScript Object Notation MBS Util Plugin 23.5 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Get or set the child node.

If the self is an empty array, we replace it with an empty object and add the value.
Raises an exception if the value is not found.
(Read and Write computed property)

See also:

JSONMBS.ChildAt(index As Integer) as JSONMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
property JavaScript Object Notation MBS Util Plugin 23.5 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Get or set child node.
Example
Var o As New JSONMBS

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

o.ChildAt(1) = JSONMBS.NewNullNode

Var v As JSONMBS = o.ChildAt(2) // see in debugger

MessageBox o.toString

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

Same as ChildAt method.
Raises exception if index is out of range.
(Read and Write computed property)

JSONMBS.Clear

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 23.5 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Clears the JSON object.
Example
Var j As New JSONMBS

j.add 1
j.add 2
j.Clear // remove all
j.add 3

MessageBox j.toString(false)

Removes all entries from array or object.

JSONMBS.Clone as JSONMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 16.0 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Clones the JSON tree.

JSONMBS.Close

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 16.0 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Frees node.

No need to call this as destructor does the same.
But this call allows you to release circular references.

JSONMBS.Compare(Other as JSONMBS) 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
Compares two objects and returns
Example
Var o As New JSONMBS

o.add 1
o.add 2

Var j As New JSONMBS

j.add 1
j.add 2

If j.Compare(o) = 0 Then
MessageBox "equal"
Else
MessageBox "not equal"
End If

Returns 0 if both are equal, 1 if bigger and -1 if smaller.

JSONMBS.Constructor

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 13.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Creates an empty object.
Example
Var c As Currency = 1.2345
Var d as Double = c

Var j As New JSONItem
j.Value("test") = d // can't use c here!

Var m As New JSONMBS
m.Value("test") = c

MessageBox j.ToString+EndOfLine+m.toString
// shows {"test":1.2344999999999999307} vs {"test":1.2345} since we store currency exactly.

See also:

JSONMBS.Constructor(dic As Dictionary)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 23.5 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Creates a new object with the content of the dictionary.
Example
Var d As New Dictionary
d.Value(1) = 2
d.Value("Hello") = "World"

Var j As New JSONMBS(d)

MessageBox j.toString(False)

Converts all dictionary values to JSON objects internally.

See also:

JSONMBS.Constructor(JSONString 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
Parses the given JSON String into the tree where this node is the root.
Example
Var o as new JSONMBS("{""text"":""Hello World""}")
MsgBox o.toString

Text should be UTF-8.
Raises an exception in case of error.

See also:

JSONMBS.Constructor(other 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
Copy constructor.

Makes a copy of all the nodes.

See also:

JSONMBS.Convert as Variant

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 19.2 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Converts JSON object to Xojo variant.
Example
// build some JSON:

Var j As New JSONMBS

j.AddItemToObject "test", JSONMBS.NewNumberNode(123)
j.AddItemToObject "name", JSONMBS.NewStringNode("Joe")

// convert to Dictionary
Var v As Variant = j.Convert

// and back
Var o As JSONMBS = JSONMBS.Convert(v)

// show
MsgBox o.toString

Depending on the JSON content, you get variants containing values (string, double, boolean or nil), dictionaries or array of variants.

Dictionary is not case sensitive, so may not work correctly if you have duplicate keys with only different in case.

With version v24.3 we also convert JSONItem and convert folderitem, date or DateTime objects to strings.

See also:

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.

Next items

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


The biggest plugin in space...