Skip to content

Commit

Permalink
docs: add tip for parsing JSON formatted outputs from commands (#2120)
Browse files Browse the repository at this point in the history
## Description:
add tip for parsing JSON formatted outputs from `command`s

## Is this change user facing?
YES

## References (if applicable):
Closes: #2088
  • Loading branch information
adschwartz authored Feb 5, 2024
1 parent 5a30ea7 commit 8cb7b0b
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions docs/docs/api-reference/starlark-reference/exec-recipe.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,47 @@ If you are trying to run a complex `command` with `|`, you should prefix the com
be rewritten as `command = ["/bin/sh", "-c", "echo a | grep a"]`. Not doing so makes everything after the `echo` as args of that command, instead of following the behavior you would expect from a shell.
:::

:::tip
If the executed command returns a proper `JSON` formatted data structure, it's necessary to pass the output through `jq`'s `fromjson` function to enable `jq` to parse the input.
For more information on `jq`'s built-in methods, plese refer to `jq`'s documentation. The following is an example of how to parse the json formatted output using `jq` syntax:

Example:
```
def run(plan, args={}):
plan.add_service(
name = "service",
config = ServiceConfig(
image = "alpine",
entrypoint = ["/bin/sh", "-c", "sleep infinity"],
)
)
cmd = ''' echo '{"key": "value"}' '''
result = plan.exec(
service_name = "service",
recipe = ExecRecipe(
command = ["/bin/sh", "-c", cmd],
extract = {
"example_reference_key": "fromjson | .key" # <----- Notice the use of `fromjson`
}
),
)
plan.print(result["output"])
plan.print(result["extract.example_reference_key"])
```

will output:
```
> print msg="{{kurtosis:1f60460f3eee4036af01b41fc2ecddc0:output.runtime_value}}"
{"key": "value"}
> print msg="{{kurtosis:1f60460f3eee4036af01b41fc2ecddc0:extract.example_reference_key.runtime_value}}"
value
```

:::


<!--------------- ONLY LINKS BELOW THIS POINT ---------------------->
[exec-reference]: ./plan.md#exec
[wait-reference]: ./plan.md#wait

0 comments on commit 8cb7b0b

Please sign in to comment.