Skip to main content

Preprocessing functions

loadJsonFile

Commentary

added in 0.4.16

Special pre-processor function that loads JSON from another file. This is useful for splitting a large configuration into pieces so you can selectively pull in fragments, like constants or entire connection blocks.

This function requires only one parameter, file, and it must be a fully-qualified path to the file inside the ShadowTraffic Docker container. So be sure to volume mount these files in addition to your base configuration.

You can also supply a data parameter to do simple primitive substitutions according to the Mustache spec. This is provided as a convenience. If you need something more sophisticated, use something like Jinja directly.


Examples

Loading a file

Specify the file with a fully qualified path.

{
"topic": "sandbox",
"value": {
"_gen": "loadJsonFile",
"file": "/path/to/file.json"
}
}

When you load JSON files, you should see messages like the following on the console:

✝ ***
✝ Replacing JSON file /path/to/file.json at path [ "generators", 0, "value", "choices" ]
✝ ***

Substitute fields

Use data to substitute primitives into the target file where names are denoted with {{ name }} expressions.

For example, /path/to/file.json should contain {{ variable }} and {{ expresssion }}, and will have "foo" and 42 substituted respectively.

{
"topic": "sandbox",
"value": {
"_gen": "loadJsonFile",
"file": "/path/to/file.json",
"data": {
"variable": "foo",
"expression": 42
}
}
}

Env variables as data

Before loadJsonFile runs, all other preprocessors run. In this example, the environment variable PORT is evaluated as data before it loads the secondary JSON file.

{
"topic": "sandbox",
"value": {
"_gen": "loadJsonFile",
"file": "/path/to/file.json",
"data": {
"port": {
"_gen": "env",
"var": "PORT"
}
}
}
}

Specification

JSON schema

{
"type": "object",
"properties": {
"file": {
"type": "string"
}
},
"required": [
"file"
]
}