Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style="literal" affects all strings in single yq command #2202

Closed
ChipWolf opened this issue Nov 24, 2024 · 0 comments
Closed

style="literal" affects all strings in single yq command #2202

ChipWolf opened this issue Nov 24, 2024 · 0 comments
Labels

Comments

@ChipWolf
Copy link

ChipWolf commented Nov 24, 2024

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.

Repository owner locked and limited conversation to collaborators Dec 7, 2024
@mikefarah mikefarah converted this issue into discussion #2220 Dec 7, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Projects
None yet
Development

No branches or pull requests

1 participant