top of page

JSONPath for Xojo


Xojo code that adds JSONPath support to the built-in JSONItem class. This extension follows RFC 9535 standards (albeit incompletely) and lets you query JSON data using simple path expressions.

Sample project that loads some JSON and lets you run test queries on it.
Sample project that loads some JSON and lets you run test queries on it.



What you can do:


  • Query JSON objects using standard JSONPath syntax

  • Get single values or entire objects

  • Use array indexing and slicing

  • Access nested properties

  • Handle errors with custom exception handling


Example 1: Basic Property Access

Var json As New JSONItem json.Load("{""name"":""John"",""age"":30}") ' Get a simple value Var name As Variant = json.Query("$.name") ' Result: John

Example 2: Array Operations

Var json As New JSONItem json.Load("{""items"":[1,2,3,4,5]}") ' Get first three items Var firstThree As JSONItem = json.Query("$.items[0:3]") ' Result: [1,2,3] ' Get last two items Var lastTwo As JSONItem = json.Query("$.items[-2:]") ' Result: [4,5] ' Get third item as Integer  Var third As Integer = json.Query("$.items[2]").IntegerValue  ' Result: 3 ' Get items in reverse order Var reversed As JSONItem = json.Query("$.items[::-1]")   ' Result: [5,4,3,2,1] ' Get every second item in reverse order Var reverseSkip As JSONItem = json.Query("$.items[::-2]")   ' Result: [5,3,1]

Example 3: Nested Objects

Var json As New JSONItem json.Load("{""store"":{""books"":[{""title"":""Book 1"",""price"":10},{""title"":""Book 2"",""price"":20}]}}") ' Get the first book's title Var title As Variant = json.Query("$.store.books[0].title") ' Result: Book 1 ' Get first two books Var books As JSONItem = json.Query("$.store.books[0:2]") ' Result: [{"title":"Book 1","price":10},{"title":"Book 2","price":20}]

License: MIT License - Free to use in personal and commercial projects. A credit would be nice but is not required. I am releasing this as open-source to the Xojo community.


Update History

Version 1.0.0 - Initial Release

Comments


Commenting has been turned off.

Copyright © Christian Wheel. All Rights Reserved.

bottom of page