jq-compatible JSON processor written in MoonBit with a CLI and TypeScript bindings
moon add shina1024/jqxnpm install @shina1024/jqxnpm install @shina1024/jqx-zod-adapter zod
npm install @shina1024/jqx-yup-adapter yup
npm install @shina1024/jqx-valibot-adapter valibot# stdin input
echo '{"foo": 1}' | jqx ".foo"
# direct input argument
jqx ".foo" '{"foo": 1}'# Raw string output (no JSON quotes)
jqx -r ".foo" '{"foo":"bar"}'
# Raw input mode (line-based strings)
jqx -R "." "a\nb"
# Raw input slurp
jqx -R -s "." "a\nb\n"
# Null input
jqx -n "."
# Slurp inputs into one array
jqx -s "." "1"
# jq -e style exit status
jqx -e ".ok" '{"ok": false}'///|
import {
"moonbitlang/core/json",
"shina1024/jqx",
}///|
test "moonbit run on standard Json" {
let input : Json = { "foo": 41.0 }
let outputs = @jqx.run(".foo + 1", input) catch {
err => fail(err.to_string())
}
assert_eq(outputs.length(), 1)
assert_eq(outputs[0].stringify(), "42")
}///|
test "moonbit compile and run through a compiled filter" {
let filter = @jqx.compile(".items[]") catch { err => fail(err.to_string()) }
let input : Json = { "items": [1.0, 2.0, 3.0] }
let outputs = filter.run(input) catch { err => fail(err.to_string()) }
assert_eq(outputs.map(v => v.stringify()), ["1", "2", "3"])
}///|
test "moonbit run_json_text preserves output text" {
let outputs = @jqx.run_json_text(".", "9007199254740993") catch {
err => fail(err.to_string())
}
assert_eq(outputs, ["9007199254740993"])
}import { run, runJsonText } from "@shina1024/jqx";
const result = run(".foo", { foo: 1 });
// { ok: true, value: [1] }
const compat = runJsonText(".", "9007199254740993");
// { ok: true, value: ["9007199254740993"] }import { compile } from "@shina1024/jqx";
const compiled = compile(".items[]");
if (compiled.ok) {
const valueLane = compiled.value.run({ items: [1, 2, 3] });
const textLane = compiled.value.runJsonText('{"items":[1,2,3]}');
}import { bindRuntime, type JqxJsonTextRuntime } from "@shina1024/jqx/bind";
const backend: JqxJsonTextRuntime = {
async runJsonText(filter, input) {
return { ok: true as const, value: [input] };
},
};
const jqx = bindRuntime(backend);
const result = await jqx.run(".", { x: 1 });import { runtime } from "@shina1024/jqx";
import { createAdapter } from "@shina1024/jqx-zod-adapter";
import { z } from "zod";
const adapter = createAdapter(runtime);
const result = await adapter.filter({
filter: ".users[].name",
input: { users: [{ name: "alice" }, { name: "bob" }] },
inputSchema: z.object({
users: z.array(z.object({ name: z.string() })),
}),
outputSchema: z.string(),
});pub suberror JsonTextRunError {
JsonParse(JsonParseError)
Compile(CompileError)
Runtime(RuntimeError)
} derive(Eq)impl Show for JsonTextRunErrorpub struct CompiledFilter {
// private fields
}fn CompiledFilter::run_json_text(self : CompiledFilter, input : StringView, max_nesting_depth? : Int) -> Array[String] raise CompiledJsonTextRunErrorfn run_json_text(filter : StringView, input : StringView, max_nesting_depth? : Int) -> Array[String] raise JsonTextRunErrorjq-compatible JSON processor written in MoonBit with a CLI and TypeScript bindings