⌘K

NativeJsonPath

A utility class for querying JSON data using a JSON Path-like syntax. This class allows parsing and querying JSON strings to extract specific values or structures.

public class NativeJsonPath 

Example Usage:

let jsonString = """
{
    "firstName": "John",
    "lastName": "doe",
    "age": 26,
    "address": {
        "streetAddress": "naist street",
        "city": "Nara",
        "postalCode": "630-0192"
    },
    "phoneNumbers": [
        {
            "type": "iPhone",
            "number": "0123-4567-8888"
        },
        {
            "type": "home",
            "number": "0123-4567-8910"
        },
        {
            "type": "office",
            "number": "0123-4567-8999"
        }
    ]
}
"""

let jsonPath = NativeJsonPath()
if let result = jsonPath.query(jsonString: jsonString, query: "$[phoneNumbers][0][number]") {
    print("Phone number: \(result)") // Output: Phone number: 0123-4567-8888
}

Initializers

init()

Initializes a new instance of NativeJsonPath.

public init() 

Methods

query(jsonString:query:)

Queries a JSON string using a JSON Path-like query.

public func query(jsonString: String?, query: String?) -> Any? 
Example Usage:
let jsonString = """
[
    {
        "postId": 1,
        "id": 1,
        "name": "id labore ex et quam laborum",
        "email": "Eliseo@gardner.biz",
        "body": "laudantium enim quasi est accusantium"
    },
    {
        "postId": 1,
        "id": 2,
        "name": "quo vero reiciendis velit similique earum",
        "email": "Jayne_Kuhic@sydney.com",
        "body": "est natus enim nihil est et"
    }
]
"""

let jsonPath = NativeJsonPath()
if let result = jsonPath.query(jsonString: jsonString, query: "$[0][name]") {
    print("Name: \(result)") // Output: Name: id labore ex et quam laborum
}

Parameters

  • jsonString: The JSON string to query.
  • query: The JSON Path-like query string.

Returns

The value or structure matching the query if found; otherwise, nil. If the query is invalid, an error message is returned.

Utility Methods

isJsonPath()

Checks if a string represents a JSON path.

public func isJsonPath() -> Bool 

Returns

A Boolean indicating whether the string is a JSON path.

parseWithJsonPath(variables:hierarchy:)

Extracts and evaluates a JSON path from a string.

public func parseWithJsonPath(variables: [String: NativeVariableModel]?, hierarchy: [NativeBlockHierarchyModel]?) -> String 
Example Usage:
let variables = ["data": NativeVariableModel(value: "[1, 2, 3]")]
let hierarchy = [NativeBlockHierarchyModel(key: "index", position: 1)]
let result = "data$[1]".parseWithJsonPath(variables: variables, hierarchy: hierarchy)
print(result) // Output: 2

Parameters

  • variables: A dictionary of variable names to their corresponding NativeVariableModel values.
  • hierarchy: An array representing the hierarchy of NativeBlockHierarchyModel items.

Returns

The evaluated string value based on the JSON path and the variables provided.

parseWithJsonPath(variables:index:)

Extracts and evaluates a JSON path from an string.

public func parseWithJsonPath(variables: [String: NativeVariableModel]?, index: Int?) -> String 
Example Usage:
let variables = ["data": NativeVariableModel(value: "[1, 2, 3]")]
let result = "data$[1]".parseWithJsonPath(variables: variables, index: 1)
print(result) // Output: 2

Parameters

  • variables: A dictionary of variable names to their corresponding NativeVariableModel values.
  • index: An optional integer representing the index value.

Returns

The evaluated string value based on the JSON path and the variables provided.