pypyr.steps.filewritejson permalink

create json file from any context object permalink

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 permalink

All inputs support substitutions. This means that you can construct the path or payload from other context variables:

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."
}

As always in pypyr, you can construct a value by embedding & combining substitution expressions in other strings:

- name: pypyr.steps.set
  comment: set some arb values in context
  in:
    set:
      arbkey: 123
      arbstr: in the middle
      filename: my-file

- name: pypyr.steps.filewritejson
  comment: write json file from substitution expressions
  in:
    fileWriteJson:
      path: out/{filename}.json
      payload:
        my_table:
          my_number: '{arbkey}'
          my_string: begin {arbstr} end

This will to output file ./out/my-file.json:

{
  "my_table": {
    "my_number": 123,
    "my_string": "begin in the middle end"
  }
}

See a worked filewritejson example.

encoding permalink

By default the file will write in the platform’s default encoding. This is utf-8 for most systems, but be aware on Windows it’s still cp1252.

You can use the encoding input explicitly to set the encoding:

- name: pypyr.steps.filewritejson
  in:
    fileWriteJson:
      path: out/utf8-example.json
      payload:
        mykey: "€ ∮ E⋅da = Q,  n → ∞, ∑ f(i) = ∏ g(i), ∀x∈ℝ: ⌈x⌉ = −⌊−x⌋, α ∧ ¬β = ¬(¬α ∨ β)"
      encoding: utf-8

See here for more details on handling text encoding in pypyr and changing the defaults.

See here for a list of available encodings.

see also

last updated on .