jq.bzlAPI docs @0.4.0

@jq.bzl//jq:jq.bzl

Load in your BUILD file with:

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

Examples

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"], )

Functions & Macros

jq

Invoke jq with a filter on a set of json input files.

Parameters
*name

Name of the rule

*srcs

List of input files. May be empty.

filter

Filter expression (https://jqlang.org/manual/#basic-filters).
Subject to stamp variable replacements, see Stamping.
When stamping is enabled, a variable named "STAMP" will be available in the filter.

Be careful to write the filter so that it handles unstamped builds, as in the example above.

Default: None
filter_file

File containing filter expression (alternative to filter)

Default: None
args

Additional args to pass to jq

Default: []
out

Name of the output json file; defaults to the rule name plus ".json"

Default: None
data

List of additional files. May be empty.

Default: []
expand_args

Run bazel's location and make variable expansion on the args.

Default: False
kwargs

Other common named parameters such as tags or visibility

jq_test

Assert that the given json files have the same semantic content.

Uses jq to filter each file. The default value of "." as the filter
means to compare the whole file.

See the jq macro for more about the filter expressions as well as
setup notes for the jq toolchain.

Note that this macro is equivalent to calling bazel_lib's diff_test with the jq outputs.

Parameters
*name

name of resulting diff_test target

*file1

a json file

*file2

another json file

filter1

a jq filter to apply to file1

Default: "."
filter2

a jq filter to apply to file2

Default: "."
kwargs

additional named arguments for the resulting diff_test

Rules

jq_rule

Most users should use the jq macro instead.

AttributeTypeDescription
*namename

A unique name for this target.

*srcslist of labels
datalist of labels
Default: []
filterstring
Default: ""
filter_filelabel
Default: None
argslist of strings
Default: []
expand_argsboolean
Default: False
outlabel
Default: None
stampinteger

Whether to encode build information into the output. Possible values:

  • stamp = 1: Always stamp the build information into the output, even in
    --nostamp builds.
    This setting should be avoided, since it is non-deterministic.
    It potentially causes remote cache misses for the target and
    any downstream actions that depend on the result.
  • stamp = 0: Never stamp, instead replace build information by constant values.
    This gives good build result caching.
  • stamp = -1: Embedding of build information is controlled by the
    --[no]stamp flag.
    Stamped targets are not rebuilt unless their dependencies change.
Default: -1

@jq.bzl//jq/toolchain:platforms.bzl

Definitions for downloading a binary for each supported platform

Repository Rules

jq_platform_repo

Fetch external tools needed for jq toolchain

AttributeTypeDescription
*namename

A unique name for this repository.

repo_mappingdictionary: String → String

In WORKSPACE context only: a dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.

For example, an entry "@foo": "@bar" declares that, for any time this repository depends on @foo (such as a dependency on @foo//some:target, it should actually resolve that dependency within globally-declared @bar (@bar//some:target).

This attribute is not supported in MODULE.bazel context (when invoking a repository rule inside a module extension's implementation function).

*versionstring
*platformstring

@jq.bzl//jq/toolchain:toolchain.bzl

Provide access to a jq binary via Bazel's toolchains feature

Rules

jq_toolchain
AttributeTypeDescription
*namename

A unique name for this target.

*binlabel

Repository Rules

jq_toolchains_repo

Creates a repository with toolchain definitions for all known platforms
which can be registered or selected.

AttributeTypeDescription
*namename

A unique name for this repository.

repo_mappingdictionary: String → String

In WORKSPACE context only: a dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.

For example, an entry "@foo": "@bar" declares that, for any time this repository depends on @foo (such as a dependency on @foo//some:target, it should actually resolve that dependency within globally-declared @bar (@bar//some:target).

This attribute is not supported in MODULE.bazel context (when invoking a repository rule inside a module extension's implementation function).

user_repository_namestring

Base name for toolchains repository

Default: ""
jq_host_alias_repo

Creates a repository with a shorter name meant for the host platform, which contains
a BUILD.bazel file that exports symlinks to the host platform's binaries

AttributeTypeDescription
*namename

A unique name for this repository.

repo_mappingdictionary: String → String

In WORKSPACE context only: a dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.

For example, an entry "@foo": "@bar" declares that, for any time this repository depends on @foo (such as a dependency on @foo//some:target, it should actually resolve that dependency within globally-declared @bar (@bar//some:target).

This attribute is not supported in MODULE.bazel context (when invoking a repository rule inside a module extension's implementation function).

Providers

JqInfo

Provide info for executing jq

Fields
bin

Executable jq binary

@jq.bzl//jq/toolchain:versions.bzl

https://github.com/stedolan/jq/releases

The integrity hashes can be computed with
shasum -b -a 384 [downloaded file] | awk '{ print $1 }' | xxd -r -p | base64