Describe the bug
When attempting to apply style="literal" to JSON string values in a YAML file using yq, the transformation unexpectedly affects all string values, not just the targeted JSON strings, even when using select() to distinguish between JSON and non-JSON values.
This behavior makes it difficult to pretty-print JSON strings without inadvertently modifying other strings in the document.
Version of yq: 4.44.5
Operating system: linux
Installed via: binary release
Input Yaml
data.yml:
json_value: '{"key1": "value1", "key2": [1, 2, 3]}'
non_json_value: "This is a plain string."
Command
The command I ran:
yq eval-all '
.. |= (select(tag == "!!str" and test("^{.*}$")) |= (fromjson | tojson | . style="literal"))
' data.yml
Actual behavior
json_value: "{\n \"key1\": \"value1\",\n \"key2\": [\n 1,\n 2,\n 3\n ]\n}"
non_json_value: "This is a plain string."
Expected behavior
json_value: |-
{
"key1": "value1",
"key2": [
1,
2,
3
]
}
non_json_value: "This is a plain string."
Workaround
A two-step yq command works as expected:
yq eval-all '
.. |= (select(tag == "!!str" and test("^{.*}$")) |= (fromjson | tojson | . style="literal"))
' data.yml | yq eval-all '
.. |= (select(tag == "!!str" and (test("^{.*}$") | not)) |= . style="")
'
This outputs:
json_value: |-
{
"key1": "value1",
"key2": [
1,
2,
3
]
}
non_json_value: This is a plain string.
Additional context
Is it possible to achieve this in a single eval-all command? Or is this a limitation of how style="literal" and select() interact within a single evaluation pass? If this is intended behavior, I’d love to understand more about why it happens.
I've also attempted to use with(), with similar results.
Describe the bug
When attempting to apply
style="literal"to JSON string values in a YAML file usingyq, the transformation unexpectedly affects all string values, not just the targeted JSON strings, even when usingselect()to distinguish between JSON and non-JSON values.This behavior makes it difficult to pretty-print JSON strings without inadvertently modifying other strings in the document.
Version of yq: 4.44.5
Operating system: linux
Installed via: binary release
Input Yaml
data.yml:
Command
The command I ran:
Actual behavior
Expected behavior
Workaround
A two-step
yqcommand works as expected:This outputs:
Additional context
Is it possible to achieve this in a single
eval-allcommand? Or is this a limitation of howstyle="literal"andselect()interact within a single evaluation pass? If this is intended behavior, I’d love to understand more about why it happens.I've also attempted to use
with(), with similar results.