SkillAgentSearch skills...

Sextant

High performance JSONPath queries for Swift

Install / Use

/learn @KittyMac/Sextant
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

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

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

Usage

import Sextant

/// Each call to Sextant's query(values: ) will return an array on success and nil on failure
func testSimple0() {
    let json = #"["Hello","World"]"#
    guard let results = json.query(values: "$[0]") else { return XCTFail() }
    XCTAssertEqualAny(results[0], "Hello")
}
/// Works with any existing JSON-like structure
func testSimple2() {
    let data = [ "Hello", "World" ]
    guard let results = data.query(values: "$[0]") else { return XCTFail() }
    XCTAssertEqualAny(results[0], "Hello")
}
/// Automatically covert to simple tuples
func testSimple3() {
    let json = #"{"name":"Rocco","age":42}"#
    
    guard let person: (name: String?, age: Int?) = json.query("$.['name','age']") else { return XCTFail() }
    XCTAssertEqual(person.name, "Rocco")
    XCTAssertEqual(person.age, 42)
}
/// Supports Decodable structs
func testSimple4() {
    let json = #"{"data":{"people":[{"name":"Rocco","age":42},{"name":"John","age":12},{"name":"Elizabeth","age":35},{"name":"Victoria","age":85}]}}"#
    
    class Person: Decodable {
        let name: String
        let age: Int
    }
    
    guard let persons: [Person] = json.query("$..[?(@.name)]") else { return XCTFail() }
    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() {
    let json1 = #"{"error":"Error format 1"}"#
    let json2 = #"{"errors":[{"title:":"Error!","detail":"Error format 2"}]}"#
            
    let queries: [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() {
    let data = #"{"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 in
        guard let json = json else { XCTFail(); return }
    
        guard let isActive: Bool = json.query("$.ServiceGroups[*][?(@.ServiceName=='Service1')].Active") else { XCTFail(); return }
        XCTAssertEqual(isActive, true)
        
        guard let 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() {
    let json = #"{"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"}]}}"#

    let modifiedJson: String? = json.parsed { root in
        guard let root = root else { XCTFail(); return nil }
        
        // Remove all females
        root.query(remove: "$..people[?(@.gender=='f')]")
        
        // Incremet all ages by 1
        root.query(map: "$..age", {
            guard let 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() {
    let json = #"{"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"}]}}"#
    
    let modifiedJson: String? = json.query(map: "$..people[*] ", { person in
        // Remove all females, increment age by 1, lowercase all names
        guard person["gender"]?.stringValue == "m" else {
            return nil
        }
        if let age = person["age"]?.intValue {
            person.set(key: "age", value: age + 1)
        }
        if let name = person["name"]?.hitchValue {
            person.set(key: "name", value: name.lowercase())
        }
        return person
    }) { root in
        return 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() {
    let oldJson = #"{"someValue": ["elem1", "elem2", "elem3"]}"#
    let newJson: String? = oldJson.query(map: "$.someValue", {_ in
        return ["elem4", "elem5"]
    } ) { root in
        return 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() {
    let oldJson = #"{"someValue": ["elem1", "elem2", "elem3"]}"#
    let replacementElement = JsonElement(unknown: ["elem4", "elem5"])
    let newJson: String? = oldJson.query(map: "$.someValue", {_ in
        return replacementElement
    } ) { root in
        return 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() {
    let json = #"[{"name":"add","inputs":[3,4]},{"name":"subtract","inputs":[6,3]},{"echo":"Hello, world"},{"name":"increment","input":41},{"echo":"Hello, world"}]"#
    
    let operations: [HalfHitch: (JsonElement) -> (Int?)] = [
        "add": { input in
            guard let values = input[element: "inputs"] else { return nil }
            guard let lhs = values[int: 0] else { return nil }
            guard let rhs = values[int: 1] else { return nil }
            return lhs + rhs
        },
        "subtract": { input in
            guard let values = input[element: "inputs"] else { return nil }
            guard let lhs = values[int: 0] else { return nil }
            guard let rhs = values[int: 1] else { return nil }
            return lhs - rhs
        },
        "increment": { input in
            guard let value = input[int: "input"] else { return nil }
            return value + 1
        }
    ]
    
    var results = [Int]()
    
    json.query(forEach: #"$[?(@.name)]"#) { operation in
        if let 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() {
    let json = #"[{"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](https://go

View on GitHub
GitHub Stars67
CategoryDevelopment
Updated1mo ago
Forks9

Languages

Swift

Security Score

95/100

Audited on Feb 17, 2026

No findings