Working with Properties

Encode/Decode/Roundtrip to/from a property file. 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.

Encode properties

Note that empty arrays and maps are not encoded by default.

Given a sample.yml file of:

# block comments come through
person: # neither do comments on maps
    name: Mike Wazowski # comments on values appear
    pets: 
    - cat # comments on array values appear
    - nested:
        - list entry
    food: [pizza] # comments on arrays do not
emptyArray: []
emptyMap: []

then

yq -o=props sample.yml

will output

Encode properties with array brackets

Declare the --properties-array-brackets flag to give array paths in brackets (e.g. SpringBoot).

Given a sample.yml file of:

then

will output

Encode properties - custom separator

Use the --properties-separator flag to specify your own key/value separator.

Given a sample.yml file of:

then

will output

Encode properties: scalar encapsulation

Note that string values with blank characters in them are encapsulated with double quotes

Given a sample.yml file of:

then

will output

Encode properties: no comments

Given a sample.yml file of:

then

will output

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:

then

will output

Decode properties

Given a sample.properties file of:

then

will output

Decode properties: numbers

All values are assumed to be strings when parsing properties, but you can use the from_yaml operator on all the strings values to autoparse into the correct type.

Given a sample.properties file of:

then

will output

Decode properties - array should be a map

If you have a numeric map key in your property files, use array_to_map to convert them to maps.

Given a sample.properties file of:

then

will output

Roundtrip

Given a sample.properties file of:

then

will output

Last updated

Was this helpful?