jq.bzl
v0.1.0
published 3 weeks ago
1 stars
0 forks
3 watchers
Apache License 2.0
public
4 assets
43,983 downloads
44 KB
Compatibility level 1
e2NDWqGcxqDP0agvvfLHovCpTbGnn/ekRp/6lChiYas=
v0.1.0
May 18, 2025

Using Bzlmod with Bazel 6 or greater

  1. (Bazel 6 only) Enable with common --enable_bzlmod in .bazelrc.
  2. Add to your MODULE.bazel file:
bazel_dep(name = "jq.bzl", version = "0.1.0")

Using WORKSPACE

Paste this snippet into your WORKSPACE.bazel file:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
    name = "jq.bzl",
    sha256 = "7b63435aa19cc6a0cfd1a82fbdf2c7a2f0a94db1a79ff7a4469ffa94286261ab",
    strip_prefix = "jq.bzl-0.1.0",
    url = "https://github.com/bazel-contrib/jq.bzl/releases/download/v0.1.0/jq.bzl-v0.1.0.tar.gz",
)
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

######################
# jq.bzl setup #
######################
http_archive(
    name = "aspect_bazel_lib",
    sha256 = "2be8a5df0b20b0ed37604b050da01dbf7ad45ad44768c0d478b64779b9f58412",
    strip_prefix = "bazel-lib-2.15.3",
    url = "https://github.com/bazel-contrib/bazel-lib/releases/download/v2.15.3/bazel-lib-v2.15.3.tar.gz",
)

load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies", "aspect_bazel_lib_register_toolchains")

# Required bazel-lib dependencies

aspect_bazel_lib_dependencies()

# Register bazel-lib toolchains

aspect_bazel_lib_register_toolchains()

What's Changed

New Contributors

Full Changelog: https://github.com/bazel-contrib/jq.bzl/commits/v0.1.0

Deps:
Assets:

Bazel jq rule

Wrapper rule around the popular jq JSON manipulation utility.

For jq documentation, see https://stedolan.github.io/jq/.

This rule was originally developed within bazel-lib. Thanks to all the contributors who made it possible!

Usage examples

load("@jq.bzl", "jq")

Create a new file bazel-out/.../no_srcs.json containing some JSON data:

jq(
    name = "no_srcs",
    srcs = [],
    filter = ".name = \"Alice\"",
)

Remove a field from package.json:

The output path bazel-out/.../package.json matches the path of the source file, which means you must refer to the label :no_dev_deps to reference the output, since Bazel doesn't provide a label for an output file that collides with an input file.

jq(
    name = "no_dev_deps",
    srcs = ["package.json"],
    out = "package.json",
    filter = "del(.devDependencies)",
)

Merge data from bar.json on top of foo.json, producing foobar.json:

jq(
    name = "merged",
    srcs = ["foo.json", "bar.json"],
    filter = ".[0] * .[1]",
    args = ["--slurp"],
    out = "foobar.json",
)

Long filters can be split over several lines with comments:

jq(
    name = "complex",
    srcs = ["a.json", "b.json"],
    filter = \"\"\"
        .[0] as $a
        # Take select fields from b.json
        | (.[1] | {foo, bar, tags}) as $b
        # Merge b onto a
        | ($a * $b)
        # Combine 'tags' array from both
        | .tags = ($a.tags + $b.tags)
        # Add new field
        + {\\\"aspect_is_cool\\\": true}
    \"\"\",
    args = ["--slurp"],
)

Load filter from a file filter.jq, making it easier to edit complex filters:

jq(
    name = "merged",
    srcs = ["foo.json", "bar.json"],
    filter_file = "filter.jq",
    args = ["--slurp"],
    out = "foobar.json",
)

Convert genquery output to JSON.

genquery(
    name = "deps",
    expression = "deps(//some:target)",
    scope = ["//some:target"],
)

jq(
    name = "deps_json",
    srcs = [":deps"],
    args = [
        "--raw-input",
        "--slurp",
    ],
    filter = "{ deps: split(\\\"\\\\n\\\") | map(select(. | length > 0)) }",
)

When Bazel is run with --stamp, replace some properties with version control info:

jq(
    name = "stamped",
    srcs = ["package.json"],
    filter = "|".join([
        # Don't directly reference $STAMP as it's only set when stamping
        # This 'as' syntax results in $stamp being null in unstamped builds.
        "$ARGS.named.STAMP as $stamp",
        # Provide a default using the "alternative operator" in case $stamp is null.
        ".version = ($stamp[0].BUILD_EMBED_LABEL // \"<unstamped>\")",
    ]),
)

jq is exposed as a "Make variable", so you could use it directly from a genrule by referencing the toolchain.

genrule(
    name = "case_genrule",
    srcs = ["a.json"],
    outs = ["genrule_output.json"],
    cmd = "$(JQ_BIN) '.' $(location a.json) > $@",
    toolchains = ["@jq_toolchains//:resolved_toolchain"],
)

"""

API docs

  • jq Run jq as a Bazel build action