Platforms to show: All Mac Windows Linux Cross-Platform
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Util/JSON/JSON Query Tests
This example is the version from Mon, 24th Sep 2023.
Project "JSON Query Tests.xojo_binary_project"
Class App Inherits ConsoleApplication
EventHandler Function Run(args() as String) As Integer
Dim json As String = "{ ""store"": {" +_
" ""book"": [ " +_
" { ""category"": ""reference""," +_
" ""author"": ""Nigel Rees""," +_
" ""title"": ""Sayings of the Century""," +_
" ""price"": 8.95" +_
" }," +_
" { ""category"": ""fiction""," +_
" ""author"": ""Evelyn Waugh""," +_
" ""title"": ""Sword of Honour""," +_
" ""price"": 12.99" +_
" }," +_
" { ""category"": ""fiction""," +_
" ""author"": ""Herman Melville""," +_
" ""title"": ""Moby Dick""," +_
" ""isbn"": ""0-553-21311-3""," +_
" ""price"": 8.99" +_
" }," +_
" { ""category"": ""fiction""," +_
" ""author"": ""J. R. R. Tolkien""," +_
" ""title"": ""The Lord of the Rings""," +_
" ""isbn"": ""0-395-19395-8""," +_
" ""price"": 22.99" +_
" }" +_
" ]," +_
" ""bicycle"": {" +_
" ""color"": ""red""," +_
" ""price"": 19.95" +_
" }" +_
" }" +_
"}"
Dim j As New JSONMBS(json)
// The authors of books that are cheaper than $10
Dim result1 As JSONMBS = j.Query("$.store.book[?(@.price < 10)].author")
print "Result 1: "+result1.ToString
// [
// "Nigel Rees",
// "Herman Melville"
// ]
// The all books
'Dim result2a As JSONMBS = j.Query("$..book")
'print "Result 2: "+result2a.ToString
// The number of books
// returns 1 currently, but should Return 4. Reported As bug...
Dim result2 As JSONMBS = j.Query("length($..book)")
print "Result 2: "+result2.ToString
// The third book
Dim result3 As JSONMBS = j.Query("$..book[2]")
print "Result 3: "+result3.ToString
// All books whose author's name starts with Evelyn
Dim result4 As JSONMBS = j.Query("$.store.book[?(@.author =~ /Evelyn.*?/)]")
print "Result 4: "+result4.ToString
// The titles of all books that have isbn number
Dim result5 As JSONMBS = j.Query("$..book[?(@.isbn)].title")
print "Result 5: "+result5.ToString
// All authors and titles of books
Dim result6 As JSONMBS = j.Query("$['store']['book']..['author','title']")
print "Result 6: "+result6.ToString
// Union of two ranges of book titles
Dim result7 As JSONMBS = j.Query("$..book[1:2,2:4].title")
print "Result 7: "+result7.ToString
// Union of a subset of book titles identified by index
Dim query8 As String = "$.store[@.book[0].title,@.book[1].title,@.book[3].title]"
Dim result8 As JSONMBS = j.Query(query8)
print "Result 8: "+result8.ToString
// Union of third book title and all book titles with price > 10
Dim result9 As JSONMBS = j.Query("$.store[@.book[3].title,@.book[?(@.price > 10)].title]")
print "Result 9: "+result9.ToString
// Intersection of book titles with category fiction and price < 15
Dim result10 As JSONMBS = j.Query("$.store.book[?(@.category == 'fiction' && @.price < 15)].title")
print "Result 10: "+result10.ToString
// Normalized path expressions
Dim result11 As JSONMBS = j.Query("$.store.book[?(@.author =~ /Evelyn.*?/)]") // , j.kPathResultOptionsPath)
print "Result 11: "+result11.ToString
// All titles whose author's second name is 'Waugh'
Dim result12 As JSONMBS = j.Query("$.store.book[?(tokenize(@.author,'\\s+')[1] == 'Waugh')].title")
print "Result 12: "+result12.ToString
// All keys in the second book
Dim result13 As JSONMBS = j.Query("keys($.store.book[1])[*]")
print "Result 13: "+result13.ToString
End EventHandler
End Class
End Project
See also:
The items on this page are in the following plugins: MBS Util Plugin.