pypyr.steps.fetchjson permalink

load & parse json permalink

Loads a json file into the pypyr context.

This step requires the following key in the pypyr context:

- name: pypyr.steps.fetchjson
  comment: fetch json from path
  in:
    fetchJson:
      path: ./path.json # required. path to file on disk. can be relative.
      key: destinationKey # optional. write json to this context key.

If you do not specify key, json writes directly to context root.

If you do not want to specify a key, you can also use the streamlined format:

- name: pypyr.steps.fetchjson
  comment: simplified input with only path
  in:
    fetchJson: ./path.json # required. path to file on disk. can be relative.

All inputs support substitutions.

See some worked examples of fetchjson.

json structure permalink

pypyr will merge the Json parsed from the file into the pypyr context. This will overwrite existing values if the same keys already exist in context.

I.e if file json has {'eggs' : 'boiled'}, but context {'eggs': 'fried'} already exists, after fetchjson the key context['eggs'] will be ‘boiled’.

If you do not specify key, the json must not be an Array [] at the root level, but rather an Object {}.

encoding permalink

By default the json file will read 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.fetchjson
  comment: set encoding
  in:
    fetchJson:
      path: testfiles/myfile.json
      key: mykey
      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 .