y
y
yq
Search…
Load
The load/strload operator allows you to load in content from another file referenced in your yaml document.
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.
Use strload to load text based content as a string block, and load to interpret the file as yaml.
Lets say there is a file ../../examples/thing.yml:
1
a: apple is included
2
b: cool
Copied!

Simple example

Given a sample.yml file of:
1
myFile: ../../examples/thing.yml
Copied!
then
1
yq eval 'load(.myFile)' sample.yml
Copied!
will output
1
a: apple is included
2
b: cool.
Copied!

Replace node with referenced file

Note that you can modify the filename in the load operator if needed.
Given a sample.yml file of:
1
something:
2
file: thing.yml
Copied!
then
1
yq eval '.something |= load("../../examples/" + .file)' sample.yml
Copied!
will output
1
something:
2
a: apple is included
3
b: cool.
Copied!

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:
1
something:
2
file: thing.yml
3
over:
4
here:
5
- file: thing.yml
Copied!
then
1
yq eval '(.. | select(has("file"))) |= load("../../examples/" + .file)' sample.yml
Copied!
will output
1
something:
2
a: apple is included
3
b: cool.
4
over:
5
here:
6
- a: apple is included
7
b: cool.
Copied!

Replace node with referenced file as string

This will work for any text based file
Given a sample.yml file of:
1
something:
2
file: thing.yml
Copied!
then
1
yq eval '.something |= strload("../../examples/" + .file)' sample.yml
Copied!
will output
1
something: |-
2
a: apple is included
3
b: cool.
Copied!
Last modified 1mo ago