jmespath

    A standards-oriented JMESPath parser and JSON query engine in MoonBit.

    jmespath
    json
    query
    parser
    Download zip
    Version
    0.1.1
    License
    Apache-2.0
    Last updated
    11 hours ago
    Downloads
    6

    #MoonJMES

    简体中文 | English

    CI License

    MoonJMES is a native MoonBit implementation of JMESPath, the standard query language for JSON. It turns nested API responses, cloud inventories, audit records, and configuration files into smaller JSON results without embedding JavaScript or shipping data to a service.

    The engine passes 892/892 cases in the official JMESPath compliance suite at pinned revision 53abcc37901891cf4308fcd910eab287416c4609. The result is reproducible with node scripts/conformance.mjs; see docs/conformance.md.

    #Why this project

    MoonBit already has general JSON and jq-style tools, but the Mooncakes search at project start returned no JMESPath implementation. JMESPath is widely used in cloud and automation tooling, has a formal grammar, and has a language-neutral test suite. That gives this project a clear ecosystem boundary and an objective definition of compatibility.

    #Features

    • identifiers, quoted identifiers, current node, JSON and raw-string literals
    • object/list wildcards, flattening, projections, filters, indexes, and slices
    • pipes, comparisons, boolean expressions, multi-select lists and hashes
    • all standard scalar, collection, conversion, and expression-reference functions, including map, sort_by, min_by, and max_by
    • reusable compiled expressions and one-shot JSON/text APIs
    • batch execution with per-request structured errors
    • configurable expression, AST depth, evaluation-step, and result limits
    • a file/stdin CLI with deterministic JSON output
    • pure MoonBit engine; Node.js is used only by the CLI host adapter

    #Quick start

    Install the published library from Mooncakes:

    moon add JingLan0v0/jmespath

    The package documentation is available at mooncakes.io/docs/JingLan0v0/jmespath.

    To run this repository's CLI, install MoonBit and Node.js, then query a file:

    moon run cmd/main --target js -- "instances[?state == 'running'].{id: id, zone: zone}" inventory.json

    Or pipe JSON through standard input:

    echo '{"items":[{"name":"A","price":9},{"name":"B","price":12}]}' \ | moon run cmd/main --target js -- "items[?price >= `10`].name" -

    Result:

    [ "B" ]

    #Library API

    Import the package in moon.pkg:

    import {
    "JingLan0v0/jmespath" @jmespath,
    "moonbitlang/core/json",
    }

    let input = @json.parse("{\"people\":[{\"name\":\"Ada\",\"age\":36}]}")
    let query = @jmespath.compile("people[?age >= `18`].name")
    let result = query.search(input)

    Public entry points: compile, Compiled::search, search, search_json, and search_batch.

    #Batch API and CLI

    Batch input is an array of independent requests:

    [ {"expression":"name","data":{"name":"Ada"}}, {"expression":"length()","data":null} ]

    moon run cmd/main --target js -- --batch requests.json

    Each response has ok: true and result, or ok: false and a structured error. A bad expression therefore does not discard other batch results.

    #Three runnable scenarios

    The examples directory covers cloud inventory selection, deployment-policy findings, and cost-report sorting. Run all three on Windows with ./examples/run.ps1; the script compares each result with committed JSON.

    #Resource limits and errors

    Defaults are 16,384 expression characters, AST depth 256, 1,000,000 evaluation steps, and 100,000 projected results. Batch input is capped at 10,000 requests; the CLI caps input at 16 MiB and performs strict UTF-8 decoding.

    Errors use stable categories: syntax, invalid_json, invalid_type, invalid_arity, invalid_value, unknown_function, invalid_batch, and limit_exceeded. A library caller receives JmesPathError; the CLI writes a single diagnostic to stderr and exits with code 2.

    #Verification

    moon fmt --check moon check --target js moon test --target js moon info --target js node scripts/conformance.mjs node scripts/verify.mjs

    The GitHub Actions matrix runs on Windows and Ubuntu with the pinned compiler. Architecture and trust boundaries are in docs/architecture.md.

    #Project boundary

    MoonJMES implements the JMESPath specification. It has no custom query extensions in version 0.1.0, which keeps results portable across compliant JMESPath implementations.

    #License

    Apache-2.0. See THIRD_PARTY_NOTICES.md for the verification-time relationship with the official test suite.

    JmesPathError

    pub(all) suberror JmesPathError {
    JmesPathError(Diagnostic)
    }

    CompareOp

    type CompareOp derive(Eq,
    Debug
    )

    Compiled

    pub(all) struct Compiled {
    source : String
    expr : Expr
    limits : Limits
    }

    Compiled::search

    fn Compiled::search(self : Compiled, data : Json) -> Json raise JmesPathError

    Compiled::source

    fn Compiled::source(self : Compiled) -> String

    Diagnostic

    pub(all) struct Diagnostic {
    code : String
    offset : Int
    message : String
    } derive(Eq,
    Debug
    )

    Limits

    pub(all) struct Limits {
    max_expression_chars : Int
    max_ast_depth : Int
    max_steps : Int
    max_results : Int
    } derive(Eq,
    Debug
    )

    TokenKind

    type TokenKind derive(Eq,
    Debug
    )

    compile

    fn compile(source : String, limits? : Limits) -> Compiled raise JmesPathError

    default_limits

    fn default_limits() -> Limits

    fn search(expression : String, data : Json, limits? : Limits) -> Json raise JmesPathError

    search_batch

    fn search_batch(requests : Json, limits? : Limits) -> Json raise JmesPathError

    Runs independent queries from a JSON array of {expression, data} objects. Errors are returned per item, so one invalid query does not abort the batch.

    search_json

    fn search_json(expression : String, json_text : String, limits? : Limits) -> Json raise JmesPathError

    version

    fn version() -> String