y
y
yq
Search…
y
y
yq
v4.x
yq
How It Works
Upgrading from V3
Commands
Evaluate
Evaluate All
Shell Completion
Operators
Usage
Output format
Working with CSV, TSV
Working with JSON
Working with Properties
Working with XML
Front Matter
Split into multiple files
GitHub Action
Tips, Tricks, Troubleshooting
Github Page
Powered By
GitBook
Working with Properties
Encode to a property file (decode not yet supported). Line comments on value nodes will be copied across.
By default, empty maps and arrays are not encoded - see below for an example on how to encode a value for these.
Note that versions prior to 4.18 require the 'eval/e' command to be specified.
yq e <exp> <file>
Encode properties
Note that empty arrays and maps are not encoded by default.
Given a sample.yml file of:
1
# block comments don't come through
2
person
:
# neither do comments on maps
3
name
:
Mike
# comments on values appear
4
pets
:
5
-
cat
# comments on array values appear
6
food
:
[
pizza
]
# comments on arrays do not
7
emptyArray
:
[]
8
emptyMap
:
[]
Copied!
then
1
yq -o
=
props sample.yml
Copied!
will output
1
# comments on values appear
2
person.name
=
Mike
3
​
4
# comments on array values appear
5
person.pets.0
=
cat
6
person.food.0
=
pizza
Copied!
Encode properties: no comments
Given a sample.yml file of:
1
# block comments don't come through
2
person
:
# neither do comments on maps
3
name
:
Mike
# comments on values appear
4
pets
:
5
-
cat
# comments on array values appear
6
food
:
[
pizza
]
# comments on arrays do not
7
emptyArray
:
[]
8
emptyMap
:
[]
Copied!
then
1
yq -o
=
props
'... comments = ""'
sample.yml
Copied!
will output
1
person.name
=
Mike
2
person.pets.0
=
cat
3
person.food.0
=
pizza
Copied!
Encode properties: include empty maps and arrays
Use a yq expression to set the empty maps and sequences to your desired value.
Given a sample.yml file of:
1
# block comments don't come through
2
person
:
# neither do comments on maps
3
name
:
Mike
# comments on values appear
4
pets
:
5
-
cat
# comments on array values appear
6
food
:
[
pizza
]
# comments on arrays do not
7
emptyArray
:
[]
8
emptyMap
:
[]
Copied!
then
1
yq -o
=
props
'(.. | select( (tag == "!!map" or tag =="!!seq") and length == 0)) = ""'
sample.yml
Copied!
will output
1
# comments on values appear
2
person.name
=
Mike
3
​
4
# comments on array values appear
5
person.pets.0
=
cat
6
person.food.0
=
pizza
7
emptyArray
=
8
emptyMap
=
Copied!
Decode properties
Given a sample.properties file of:
1
# comments on values appear
2
person.name
=
Mike
3
​
4
# comments on array values appear
5
person.pets.0
=
cat
6
person.food.0
=
pizza
Copied!
then
1
yq -p
=
props sample.properties
Copied!
will output
1
person
:
2
name
:
Mike
# comments on values appear
3
pets
:
4
-
cat
# comments on array values appear
5
food
:
6
-
pizza
Copied!
Roundtrip
Given a sample.properties file of:
1
# comments on values appear
2
person.name
=
Mike
3
​
4
# comments on array values appear
5
person.pets.0
=
cat
6
person.food.0
=
pizza
Copied!
then
1
yq -p
=
props -o
=
props
'.person.pets.0 = "dog"'
sample.properties
Copied!
will output
1
# comments on values appear
2
person.name
=
Mike
3
​
4
# comments on array values appear
5
person.pets.0
=
dog
6
person.food.0
=
pizza
Copied!
Usage - Previous
Working with JSON
Next - Usage
Working with XML
Last modified
2mo ago
Copy link
Contents
Encode properties
Encode properties: no comments
Encode properties: include empty maps and arrays
Decode properties
Roundtrip