Converting JSON string to JObject and parsing it via JPath node in Grasshopper.

Converting JSON string to JObject and parsing it via JPath node in Grasshopper.

ghJSON is a set of JSON components for Grasshopper, based on perfect Json.NET  and JsonPath libraries.

JPath (JSONPath) is an alternative to XPath (XML Path Language). It's paradigm better fits functional programming pipeline, because you don't need to do multiple iterations over complex data set to extract all nested parameters.

To use JSONPath, all you need to do is write a query like $[*].title. Where $ is the root of Json document, store is an object name, book is the nested object and title is the title of a book. ([*] Operator allows us to select all books from the document, not only the first one.)

Source code is available on GitHub.

JSONPath Examples

Sample JSON

{ "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

Example JSONPath Queries

XPath JSONPath Result
/store/book/author $[*].author The authors of all books in the store.
//author $ All authors.
/store/* $.store.* All things in store, which are some books and a red bicycle.
/store//price $.store..price The price of everything in the store.
//book[3] $[2] The third book.
//book[last()] $[(@.length-1)]
The last book in order.
//book[position()<3] $[0,1]
The first two books.
//book[isbn] $[?(@.isbn)] Filter all books with isbn number.
//book[price<10] $[?(@.price<10)] Filter all books cheapier than 10.
//* $..* All Elements in XML document. All members of JSON structure.

Comparison Between XPath and JSONPath Syntax

XPath JSONPath Description
/ $ The root object/element.
. @ The current object/element.
/ . or [] Child operator.
.. n/a Parent operator.
// .. Recursive descent. JSONPath borrows this syntax from E4X.
* * Wildcard. All objects/elements regardless their names.
@ n/a Attribute access. JSON structures don't have attributes.
[] [] Subscript operator. XPath uses it to iterate over element collections and for predicates. In Javascript and JSON it is the native array operator.
| [,] Union operator in XPath results in a combination of node sets. JSONPath allows alternate names or array indices as a set.
n/a [start:end:step] Array slice operator borrowed from ES4.
[] ?() Applies a filter (script) expression.
n/a () Script expression, using the underlying script engine.
() n/a Grouping in Xpath