Load
The load operators allows you to load in content from another file.
Note that you can use string operators like + and sub to modify the value in the yaml file to a path that exists in your system.
You can load files of the following supported types:
Yaml
load
XML
load_xml
Properties
load_props
Plain String
load_str
Base64
load_base64
Note that load_base64 only works for base64 encoded utf-8 strings.
Samples files for tests:
yaml
../../examples/thing.yml:
a: apple is included
b: coolxml
small.xml:
<this>is some xml</this>properties
small.properties:
this.is = a properties filebase64
base64.txt:
bXkgc2VjcmV0IGNoaWxsaSByZWNpcGUgaXMuLi4uSimple example
Given a sample.yml file of:
myFile: ../../examples/thing.ymlthen
yq 'load(.myFile)' sample.ymlwill output
a: apple is included
b: cool.Replace node with referenced file
Note that you can modify the filename in the load operator if needed.
Given a sample.yml file of:
something:
file: thing.ymlthen
yq '.something |= load("../../examples/" + .file)' sample.ymlwill output
something:
a: apple is included
b: cool.Replace all nodes with referenced file
Recursively match all the nodes (..) and then filter the ones that have a 'file' attribute.
Given a sample.yml file of:
something:
file: thing.yml
over:
here:
- file: thing.ymlthen
yq '(.. | select(has("file"))) |= load("../../examples/" + .file)' sample.ymlwill output
something:
a: apple is included
b: cool.
over:
here:
- a: apple is included
b: cool.Replace node with referenced file as string
This will work for any text based file
Given a sample.yml file of:
something:
file: thing.ymlthen
yq '.something |= load_str("../../examples/" + .file)' sample.ymlwill output
something: |-
a: apple is included
b: cool.Load from XML
Given a sample.yml file of:
cool: thingsthen
yq '.more_stuff = load_xml("../../examples/small.xml")' sample.ymlwill output
cool: things
more_stuff:
this: is some xmlLoad from Properties
Given a sample.yml file of:
cool: thingsthen
yq '.more_stuff = load_props("../../examples/small.properties")' sample.ymlwill output
cool: things
more_stuff:
this:
is: a properties fileMerge from properties
This can be used as a convenient way to update a yaml document
Given a sample.yml file of:
this:
is: from yaml
cool: aythen
yq '. *= load_props("../../examples/small.properties")' sample.ymlwill output
this:
is: a properties file
cool: ayLoad from base64 encoded file
Given a sample.yml file of:
cool: thingsthen
yq '.more_stuff = load_base64("../../examples/base64.txt")' sample.ymlwill output
cool: things
more_stuff: my secret chilli recipe is....Last updated
Was this helpful?