pypyr.parser.json permalink

parse json input string from cli permalink

Takes a json string from the cli argument input, parses it into a type-safe json object, and puts the object into the pypyr context.

pypyr honors the input json data types. In other words, bools are bools, numbers are numbers, nulls are nulls.

example permalink

Given a pipeline like this, arbitrarily saved as ./json-parser.yaml:

# ./json-parser.yaml
context_parser: pypyr.parser.json
steps:
  # echoMe will be set in the input json str
  - pypyr.steps.echo
  - name: pypyr.steps.set
    comment: use the int as a strongly typed int
    in:
      set:
        intResult: !py keyInt * 2

  - name: pypyr.steps.echo
    comment: print out result of int calculation
    in:
      echoMe: "strongly typed happens automatically: {intResult}"

  - name: pypyr.steps.echo
    comment: only run if input bool is true
    run: '{keyBool}'
    in:
      echoMe: you'll only see me if the bool was true

You can run this pipeline as follows:

$ pypyr json-parser '{"echoMe": "hello from json", "keyInt": 123, "keyBool": true}'
hello from json
strongly typed happens automatically: 246
you'll only see me if the bool was true

If you change the bool to false:

$ pypyr json-parser '{"echoMe": "hello from json", "keyInt": 123, "keyBool": false}'
hello from json
strongly typed happens automatically: 246

see also

last updated on .