Platforms to show: All Mac Windows Linux Cross-Platform

Back to JSONMBS class.

Previous items

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)

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.

JSONMBS.Search(Path as string) 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 JMESPath query.
Example
Var json As String = "{ ""locations"": ["+EndOfLine+_
"{""name"": ""Seattle"", ""state"": ""WA""},"+EndOfLine+_
"{""name"": ""New York"", ""state"": ""NY""},"+EndOfLine+_
"{""name"": ""Bellevue"", ""state"": ""WA""},"+EndOfLine+_
"{""name"": ""Olympia"", ""state"": ""WA""}"+EndOfLine+_
"]}"
Var query As String = "locations[?state == 'WA'].name | sort(@) | {WashingtonCities: join(', ', @)}"

Var j As New JSONMBS(json)
Var r As JSONMBS = j.Search(query)

MessageBox r.toString
// { "WashingtonCities": "Bellevue, Olympia, Seattle" }

The jmespath extension implements JMESPath. JMESPath is a query language for transforming JSON documents into other JSON documents. It's supported in both the AWS and Azure CLI and has libraries available in a number of languages.

To learn more about JMESPath, please check this website:
https://jmespath.org

See also Query() for queries with JSONPath expressions.

JSONMBS.Sort(Reverse as boolean = false)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 23.5 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Sorts values in the array by values or objects by key names.
Example
Var o As New JSONMBS

o.add 3
o.add 4
o.Add 1

o.Sort

MessageBox o.toString(False) // shows 1,3,4

Reverse can be set to true in order to reverse the order.

JSONMBS.ToHTML(NoHeader as boolean = false, CSS as string = "") as String

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 18.2 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Converts JSON to HTML.

We build for you a HTML with tables for each array and object. We include values and tag rows with even/odd CSS classes.
If NoHeader is true, you get just the raw table without header/footer.
Anything in CSS parameter is inserted before the table.
Returns HTML, which can be loaded in htmlviewer.

Example for CSS to do even/odd line backgrounds:

/* CSS style to include */
"<style>
td
{
vertical-align:top;
}

.odd
{
background-color: white;
}

.even
{
background-color: #DDD;
}
</style>"

Some examples using this method:

JSONMBS.toString(formatted as boolean) as string

Type Topic Plugin Version macOS Windows Linux iOS Targets
method JavaScript Object Notation MBS Util Plugin 13.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Produces a JSON markup text document from a document tree.
Example
Var o as JSONMBS = JSONMBS.NewObjectNode

o.AddItemToObject "text", JSONMBS.NewStringNode("Hello World")

MsgBox o.toString // shows "{"text":"Hello World"}"

Returns "" on any error. Lasterror is set.

See also:

JSONMBS.Value(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.value(0) = "Hello"
o.value(1) = "World"
o.value(2) = "test"

MessageBox o.toString
MessageBox o.Value(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.
Raises exception if index is out of range.
(Read and Write computed property)

See also:

JSONMBS.Value(Key As String) 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 a value in an object by key name.
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 MBS stores currency exactly.

Variant is converted to or from JSONMBS if needed.
Return value as variant.
Raises an exception if the value is not found.
(Read and Write computed property)

See also:

JSONMBS.ValueAt(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.ValueAt(0) = "Hello"
o.ValueAt(1) = "World"
o.ValueAt(2) = "test"

MessageBox o.toString
MessageBox o.ValueAt(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 set the value.

Same as Value or Operator_Subscript.
Raises exception if index is out of range.
(Read and Write computed property)

JSONMBS.Values 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.

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

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

Previous items

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


The biggest plugin in space...