Skip to content

Repository files navigation

Sextant

Sextant is a complete, high performance JSONPath implementation written in Swift. It was originally ported from SMJJSONPath, which in turn is a tight adaptation of the Jayway JsonPath implementation. Sextant has since been updated to bring it into compliance with other JSON path implementations (see issue), so this specific implementation now varies from the SMJJSONPath/Jayway implementation.

Getting Started

Goals

  • Simple API
  • Full JSONPath implementation
  • Modification of paths ( use map/filter/remove/forEach )
  • High performance
  • Linux support

Usage

import Sextant
/// Each call to Sextant's query(values: ) will return an array on success and nil on failure
func testSimple0(){letjson=#"["Hello","World"]"#guardlet results = json.query(values:"$[0]")else{returnXCTFail()}XCTAssertEqualAny(results[0],"Hello")}
/// Works with any existing JSON-like structure
func testSimple2(){letdata=["Hello","World"]guardlet results = data.query(values:"$[0]")else{returnXCTFail()}XCTAssertEqualAny(results[0],"Hello")}
/// Automatically covert to simple tuples
func testSimple3(){letjson=#"{"name":"Rocco","age":42}"#guardlet person:(name:String?, age:Int?)= json.query("$.['name','age']")else{returnXCTFail()}XCTAssertEqual(person.name,"Rocco")XCTAssertEqual(person.age,42)}
/// Supports Decodable structs
func testSimple4(){letjson=#"{"data":{"people":[{"name":"Rocco","age":42},{"name":"John","age":12},{"name":"Elizabeth","age":35},{"name":"Victoria","age":85}]}}"#classPerson:Decodable{letname:Stringletage:Int}guardlet persons:[Person]= json.query("$..[?(@.name)]")else{returnXCTFail()}XCTAssertEqual(persons[0].name,"Rocco")XCTAssertEqual(persons[0].age,42)XCTAssertEqual(persons[2].name,"Elizabeth")XCTAssertEqual(persons[2].age,35)}
/// Easily combine results from multiple queries
func testSimple5(){letjson1=#"{"error":"Error format 1"}"#letjson2=#"{"errors":[{"title:":"Error!","detail":"Error format 2"}]}"#letqueries:[String]=["$.error","$.errors[0].detail",]XCTAssertEqualAny(json1.query(string: queries),"Error format 1")XCTAssertEqualAny(json2.query(string: queries),"Error format 2")}
/// High performance JSON processing by using .parsed() to get
/// a quick view of the raw json to execute on paths on
func testSimple9(){letdata=#"{"DtCutOff":"2018-01-01 00:00:00","ServiceGroups":[{"ServiceName":"Service1","DtUpdate":"2021-11-22 00:00:00","OrderNumber":"123456","Active":"true"},{"ServiceName":"Service2","DtUpdate":"2021-11-20 00:00:00","OrderNumber":"123456","Active":true},{"ServiceName":"Service3","DtUpdate":"2021-11-10 00:00:00","OrderNumber":"123456","Active":false}]}"#
data.parsed{ json inguardlet json = json else{XCTFail(); return}guardlet isActive:Bool= json.query("$.ServiceGroups[*][?(@.ServiceName=='Service1')].Active")else{XCTFail(); return}XCTAssertEqual(isActive,true)guardlet date:Date= json.query("$.ServiceGroups[*][?(@.ServiceName=='Service1')].DtUpdate")else{XCTFail(); return}XCTAssertEqual(date,"2021-11-22 00:00:00".date())}}
/// Use replace, map, filter, remove and forEach to perform mofications to your json
func testSimple10(){letjson=#"{"data":{"people":[{"name":"Rocco","age":42,"gender":"m"},{"name":"John","age":12,"gender":"m"},{"name":"Elizabeth","age":35,"gender":"f"},{"name":"Victoria","age":85,"gender":"f"}]}}"#letmodifiedJson:String?= json.parsed{ root inguardlet root = root else{XCTFail(); returnnil}
// Remove all females
root.query(remove:"$..people[?(@.gender=='f')]")
// Incremet all ages by 1
root.query(map:"$..age",{guardlet age = $0.intValue else{return $0 }return age +1})
// Lowercase all names
root.query(map:"$..name",{ $0.hitchValue?.lowercase()})return root.description
}XCTAssertEqual(modifiedJson,#"{"data":{"people":[{"name":"rocco","age":43,"gender":"m"},{"name":"john","age":13,"gender":"m"}]}}"#)}
/// Use a single map to accomplish the same task as above but with only one pass through the data
func testSimple11(){letjson=#"{"data":{"people":[{"name":"Rocco","age":42,"gender":"m"},{"name":"John","age":12,"gender":"m"},{"name":"Elizabeth","age":35,"gender":"f"},{"name":"Victoria","age":85,"gender":"f"}]}}"#letmodifiedJson:String?= json.query(map:"$..people[*] ",{ person in
// Remove all females, increment age by 1, lowercase all names
guardperson["gender"]?.stringValue =="m"else{returnnil}iflet age =person["age"]?.intValue {
person.set(key:"age", value: age +1)}iflet name =person["name"]?.hitchValue {
person.set(key:"name", value: name.lowercase())}return person
}){ root inreturn root.description
}XCTAssertEqual(modifiedJson,#"{"data":{"people":[{"name":"rocco","age":43,"gender":"m"},{"name":"john","age":13,"gender":"m"}]}}"#)}
/// You are not bound to just modify existing elements in your JSON,
/// you can return any json-like structure in your mapping
func testSimple12(){letoldJson=#"{"someValue": ["elem1", "elem2", "elem3"]}"#letnewJson:String?= oldJson.query(map:"$.someValue",{_ inreturn["elem4","elem5"]}){ root inreturn root.description
}XCTAssertEqual(newJson,#"{"someValue":["elem4","elem5"]}"#)}
/// For the performance minded, your maps should do as little work as possible
/// per replacement. To improve on the previous example, we could create our
/// replacement element outside of the mapping to reduce unnecessary work.
func testSimple13(){letoldJson=#"{"someValue": ["elem1", "elem2", "elem3"]}"#letreplacementElement=JsonElement(unknown:["elem4","elem5"])letnewJson:String?= oldJson.query(map:"$.someValue",{_ inreturn replacementElement
}){ root inreturn root.description
}XCTAssertEqual(newJson,#"{"someValue":["elem4","elem5"]}"#)}
/// Example of handling an heterogenous array. The task is to iterate over all
/// operations and perform a dynamic lookup to the operation function, perform
/// the task and coallate the results.
func testSimple14(){letjson=#"[{"name":"add","inputs":[3,4]},{"name":"subtract","inputs":[6,3]},{"echo":"Hello, world"},{"name":"increment","input":41},{"echo":"Hello, world"}]"#letoperations:[HalfHitch:(JsonElement)->(Int?)]=["add":{ input inguardlet values =input[element:"inputs"]else{returnnil}guardlet lhs =values[int:0]else{returnnil}guardlet rhs =values[int:1]else{returnnil}return lhs + rhs
},"subtract":{ input inguardlet values =input[element:"inputs"]else{returnnil}guardlet lhs =values[int:0]else{returnnil}guardlet rhs =values[int:1]else{returnnil}return lhs - rhs
},"increment":{ input inguardlet value =input[int:"input"]else{returnnil}return value +1}]varresults=[Int]()
json.query(forEach:#"$[?(@.name)]"#){ operation iniflet opName =operation[halfHitch:"name"],let opFunc =operations[opName]{
results.append(opFunc(operation)??0)}}XCTAssertEqual(results,[7,3,42])}
/// You can test a json path for validity by calling .query(validate)
func testSimple17(){letjson=#"[{"title":"Post 1","timestamp":1},{"title":"Post 2","timestamp":2}]"#XCTAssertEqual(json.query(validate:"$"),nil)XCTAssertEqual(json.query(validate:""),"Path must start with $ or @")XCTAssertEqual(json.query(validate:"$."),"Path must not end with a \'.\' or \'..\'")XCTAssertEqual(json.query(validate:"$.."),"Path must not end with a \'.\' or \'..\'")XCTAssertEqual(json.query(validate:"$.store.book[["),"Could not parse token starting at position 12.")}

Performance

Sextant utilizes Hitch (high performance strings) and Spanker (high performance, low overhead JSON deserialization) to provide a best-in-class JSONPath implementation for Swift. Hitch allows for fast, utf8 shared memory strings. Spanker generates a low cost view of the JSON blob which Sextant then queries the JSONPath against. Nothing is deserialized and no memory is copied from the source JSON blob until they are returned as results from the query. Sextant really shines in scenarios where you have a large amount of JSON and/or a large number of queries to run against it.

Installation

Sextant is fully compatible with the Swift Package Manager

dependencies: [
.package(url: "https://github.com/KittyMac/Sextant.git", .upToNextMinor(from: "0.4.0"))
],

What is JSONPath

The original Stefan Goessner JsonPath implemenentation was released in 2007, and from it spawned dozens of different implementations. This JSONPath Comparison chart shows the wide array of available implemenations, and at the time of this writing a Swift implementation is not present (note that there exists the SwiftPath project, but it is not included in said chart due to critical errors when running on Linux.

The rest of this section is largely adapted from the Jayway JsonPath Getting Started section.

Operators

OperatorDescription
$The root element to query. This starts all path expressions.
@The current node being processed by a filter predicate.
*Wildcard. Available anywhere a name or numeric are required.
..Deep scan. Available anywhere a name is required.
.<name>Dot-notated child
['<name>' (, '<name>')]Bracket-notated child or children
[<number> (, <number>)]Array index or indexes
[start:end]Array slice operator
[?(<expression>)]Filter expression. Expression must evaluate to a boolean value.

Functions

Functions can be invoked at the tail end of a path - the input to a function is the output of the path expression. The function output is dictated by the function itself.

FunctionDescriptionOutput type
min()Provides the min value of an array of numbersDouble
max()Provides the max value of an array of numbersDouble
avg()Provides the average value of an array of numbersDouble
stddev()Provides the standard deviation value of an array of numbersDouble
length()Provides the length of an arrayInteger
sum()Provides the sum value of an array of numbersDouble
keys()Provides the property keys (An alternative for terminal tilde ~)Set<E>
concat(X)Provides a concatinated version of the path output with a new itemlike input
append(X)add an item to the json path output arraylike input

Filter Operators

Filters are logical expressions used to filter arrays. A typical filter would be [?(@.age > 18)] where @ represents the current item being processed. More complex filters can be created with logical operators && and ||. String literals must be enclosed by single or double quotes ([?(@.color == 'blue')] or [?(@.color == "blue")]).

OperatorDescription
==left is equal to right (note that 1 is not equal to '1')
!=left is not equal to right
<left is less than right
<=left is less or equal to right
>left is greater than right
>=left is greater than or equal to right
=~left matches regular expression [?(@.name =~ /foo.*?/i)]
inleft exists in right [?(@.size in ['S', 'M'])]
ninleft does not exists in right
subsetofleft is a subset of right [?(@.sizes subsetof ['S', 'M', 'L'])]
anyofleft has an intersection with right [?(@.sizes anyof ['M', 'L'])]
noneofleft has no intersection with right [?(@.sizes noneof ['M', 'L'])]
sizesize of left (array or string) should match right
emptyleft (array or string) should be empty

Path Examples

Given the json

{"store": {"book": [{"category": "reference","author": "Nigel Rees","title": "Sayings of the Century","display-price": 8.95,"bargain": true},{"category": "fiction","author": "Evelyn Waugh","title": "Sword of Honour","display-price": 12.99,"bargain": false},{"category": "fiction","author": "Herman Melville","title": "Moby Dick","isbn": "0-553-21311-3","display-price": 8.99,"bargain": true},{"category": "fiction","author": "J. R. R. Tolkien","title": "The Lord of the Rings","isbn": "0-395-19395-8","display-price": 22.99,"bargain": false}],"bicycle": {"color": "red","display-price": 19.95,"foo:bar": "fooBar","dot.notation": "new","dash-notation": "dashes"}}}
JsonPath (click link to try)Result
$.store.book[*].authorThe authors of all books
$..['author','title']All authors and titles
$.store.*All things, both books and bicycles
$.store..display-priceThe price of everything
$..book[2]The third book
$..book[-2]The second to last book
$..book[0,1]The first two books
$..book[:2]All books from index 0 (inclusive) until index 2 (exclusive)
$..book[1:2]All books from index 1 (inclusive) until index 2 (exclusive)
$..book[-2:]Last two books
$..book[2:]Book number two from tail
$..book[?(@.isbn)]All books with an ISBN number
$.store.book[?(@.display-price < 10)]All books in store cheaper than 10
$..book[?(@.bargain == true)]All bargain books in store
$..book[?(@.author =~ /.*REES/i)]All books matching regex (ignore case)
$..*Give me every thing
$..book.length()The number of books

License

Sextant is free software distributed under the terms of the MIT license, reproduced below. Sextant may be used for any purpose, including commercial purposes, at absolutely no cost. No paperwork, no royalties, no GNU-like "copyleft" restrictions. Just download and enjoy.

Copyright (c) 2021 Chimera Software, LLC

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

High performance JSONPath queries for Swift

Resources

Stars

68 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages