pypyr.steps.filewritejson
create json file from any context object
Format & write a payload to a json file on disk. This is useful for generating json files from your pipeline such as when you want to create configuration files dynamically on the fly.
filewritejson
works like this:
- name: pypyr.steps.filewritejson
comment: write context payload out to json
in:
fileWriteJson:
path: /path/to/output.json # destination file
payload: # (optional) payload to write to path
key1: value1 # output json will have
key2: value2 # key1 and key2 as string
key3: 124 # output int
key4: false # output bool
This will generate the following json to /path/to/output.json
:
{
"key1": "value1",
"key2": "value2",
"key3": 123,
"key4": false
}
Note that the correct data types will serialize to the generated output json. More complicated hierarchical nested structures will also just work.
If you do not specify payload
, pypyr will write the entire context to
the output file in json format. Be careful if you have sensitive values
like passwords or private keys!
format json with token replacements
All inputs support substitutions. This means that you can specify another context item to be the path and/or the payload, for example:
arbkey: arbvalue
writehere: /path/to/output.json
writeme:
this: json content
will: be written to
thepath: with substitutions like this {arbkey}.
fileWriteJson:
path: '{writehere}'
payload: '{writeme}'
Substitution processing also runs on the output payload content.
In the above example, in the output json file created at /path/to/output.json
,
the {arbkey}
expression in the last line will substitute like this:
{
"this": "json content",
"will": "be written to",
"thepath": "with substitutions like this arbvalue."
}
See a worked filewritejson example.