Platforms to show: All Mac Windows Linux Cross-Platform

Back to JSONMBS class.

Previous items Next items

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.

JSONMBS.Load(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
o.load("{""text"":""Hello World""}")
MsgBox o.toString

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

JSONMBS.Lookup(Key As String, defaultValue As Variant = nil) 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
Looks up the value for the key.

Variant is converted to JSONMBS if needed.
Returns default value if key is not found.

JSONMBS.Merge(Other as JSONMBS)   New in 24.2

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 24.2 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Merges two JSON objects or arrays.
Example
// merge two arrays
Var j1 As New JSONMBS("[1,2,3]")
Var j2 As New JSONMBS("[4,2,5]")

j1.Merge(j2)

MessageBox j1.toString(False)

// merge two objects
Var j3 As New JSONMBS("{""a"": 1, ""b"": [1]}")
Var j4 As New JSONMBS("{""c"": 3, ""b"": [2]}")

j3.Merge(j4)

MessageBox j3.toString(True)

For arrays, we check if the value is already there and if not, we add it.
For objects, we check the values for each key. If it doesn't exist, we copy it over. If it exists, we check arrays and objects again, but other values just get replaced.

Raises exception if you try to merge object with array.

JSONMBS.Name(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.Name(0)

Raises an exception if index is out of range.

Same as NameAt or KeyAt methods.

See also:

JSONMBS.NameAt(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.NameAt(0)

Raises an exception if index is out of range.

Same as Name or KeyAt methods.

JSONMBS.Names 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 Names() As String = o.Names
MessageBox string.FromArray(Names)

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 Keys method.

JSONMBS.Operator_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 JSONMBS objects.
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 = o Then
MessageBox "equal"
Else
MessageBox "not equal"
End If

Called automatically by Xojo if you use =, > and < operators.
Same as Compare method.

JSONMBS.Operator_Convert 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
Queries all values as an array.
Example
Var o As New JSONMBS

o.add 1
o.add 2

// auto convert on assignment
Var values() As Variant = o
Break // see debugger

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

Works for both objects and arrays.
Same as Values method.

See also:

JSONMBS.Operator_Convert(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 JSONMBS = d

MessageBox j.toString(False)

Converts all dictionary values to JSON objects internally.

See also:

JSONMBS.Operator_Subscript(index As Integer) as Variant

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 value at given index.
Example
Var o As New JSONMBS

o(0) = "Hello"
o(1) = "World"
o(2) = "test"

MessageBox o.toString
MessageBox o(1)

Variant is converted to JSONMBS if needed.
Index must be in range from 0 to Count. If index is equal to count, we append the value on setting.

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

Same as ValueAt or Operator_Subscript.
(Read and Write computed property)

Some examples using this property:

JSONMBS.Query(Path as string, Options as Integer = 0) 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
Performs a JSON Path query.
Example
Var book1 As New JSONMBS
book1.Value("title") = "Sayings Of the Century"
book1.Value("price") = 8.95

Var book2 As New JSONMBS
book2.Value("title") = "Sword Of Honour"
book2.Value("price") = 12.99

Var bookArray As New JSONMBS
bookArray.Append book1
bookArray.Append book2

Var j As New JSONMBS
j.Value("book") = bookArray

MessageBox j.toString(True)
// shows
// {
// "book": [
// {
// "title": "Sayings Of the Century",
// "price": 8.95
// },
// {
// "title": "Sword Of Honour",
// "price": 12.99
// }
// ]
// }

Var r as JSONMBS = j.Query("$.book[?(@.price < 10)].title")
Var s As String = r.toString

MessageBox s
// shows ["Sayings Of the Century"]

Evaluates the root value against the JSONPath expression and returns an array of values or normalized path expressions.

Returns a JSON with an array containing either values or normalized path expressions matching the JSONPath expression, or an empty array if there is no match.

For options use kPathResultOptions* constants. Use OR to combine multiple flags.

kPathResultOptionsValueReturn values.
kPathResultOptionsNoDuplicatesRemove duplicates.
kPathResultOptionsSortSort results.
kPathResultOptionsPathReturn paths.

To learn more about JSONPath, please check this website:
https://goessner.net/articles/JsonPath/

Learn more about the JSONPath implementation here:
https://danielaparker.github.io/JsonCons.Net/articles/JsonPath/JsonConsJsonPath.html

See also Replace() to replace found values with new values.

JSONMBS.Remove(Index 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
Removes an entry by index.
Example
Var o As New JSONMBS

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

MessageBox o.toString(False)

// remove middle item
o.Remove 1

MessageBox o.toString(false)

Works for arrays or objects.
For objects, prefer to delete by key name since removing an entry changes the order.

Same as RemoveAt method.

See also:

JSONMBS.Remove(Key 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
Removes the node with the given name.

Only for JSONMBS of type object.

See also:

JSONMBS.RemoveAt(Index 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
Removes an entry by index.
Example
Var o As New JSONMBS

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

MessageBox o.toString(False)

// remove middle item
o.RemoveAt 1

MessageBox o.toString(false)

Works for arrays or objects.
For objects, prefer to delete by key name since removing an entry changes the order.

Same as Remove method.

JSONMBS.Replace(Path as string, NewValue as Variant) 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
Searches for all values that match the JSONPath expression and replaces them with the specified value.
Example
Var json As String = "{" +_
" ""books"":" +_
" [" +_
" {" +_
" ""category"": ""fiction""," +_
" ""title"" : ""A Wild Sheep Chase""," +_
" ""author"" : ""Haruki Murakami""," +_
" ""price"" : 22.72" +_
" }," +_
" {" +_
" ""category"": ""fiction""," +_
" ""title"" : ""The Night Watch""," +_
" ""author"" : ""Sergei Lukyanenko""," +_
" ""price"" : 23.58" +_
" }," +_
" {" +_
" ""category"": ""fiction""," +_
" ""title"" : ""The Comedians""," +_
" ""author"" : ""Graham Greene""," +_
" ""price"" : 21.99" +_
" }," +_
" {" +_
" ""category"": ""memoir""," +_
" ""title"" : ""The Night Watch""," +_
" ""author"" : ""Phillips, David Atlee""" +_
" }" +_
" ]" +_
"}"

Var j As New JSONMBS(json)
Var n As JSONMBS = j.Replace("$.books[?(@.title == 'A Wild Sheep Chase')].price", 123.0)

MessageBox n.toString(True)

Throws a JSONExceptionMBS if JSONPath evaluation fails.
Returns the modified copy of the JSON.

See also Query() function to just search without replace.

Previous items Next items

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


The biggest plugin in space...