Skip to content
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

Duplicated values when appending to array #1145

Closed
crisbal opened this issue Mar 18, 2022 · 5 comments
Closed

Duplicated values when appending to array #1145

crisbal opened this issue Mar 18, 2022 · 5 comments
Labels

Comments

@crisbal
Copy link

crisbal commented Mar 18, 2022

Describe the bug

Input:

- children: ["c1"]
- children: []
- children: []
- children: ["c1", "c2"]

Query:

.[] | .children += ["a"]

Result:

children: ["c1", a, a, a, a]
children:
  - a
  - a
  - a
  - a
children:
  - a
  - a
  - a
  - a
children: ["c1", "c2", a, a, a, a]

Expected result:

- children: ["c1", "a"]
- children: ["a"]
- children: ["a"]
- children: ["c1", "c2", "a"]

Version of yq: 4.22.1
Operating system: linux
Installed via: binary


jq behaviour

yq -o json /tmp/bug.yml | jq '.[] | .children += ["a"]'
{
  "children": [
    "c1",
    "a"
  ]
}
{
  "children": [
    "a"
  ]
}
{
  "children": [
    "a"
  ]
}
{
  "children": [
    "c1",
    "c2",
    "a"
  ]
}
@crisbal
Copy link
Author

crisbal commented Mar 18, 2022

The following works instead.

>>> yq '.[].children += ["a"]' /tmp/bug.yml
- children: ["c1", a]
- children:
    - a
- children:
    - a
- children: ["c1", "c2", a]

@crisbal
Copy link
Author

crisbal commented Mar 18, 2022

More interesting use case

Input:

- type: "skip"
  children: ["c1"]
- type: "skip"
  children: []
- type: "ok"
  children: []
- type: "ok"
  children: ["c1", "c2"]

Query:

yq 'map(select(.type != "skip").children += ["a"])' /tmp/bug.yml

Output:

- type: "skip"
  children: ["c1"]
- type: "skip"
  children: []
- type: "ok"
  children:
    - a
    - a
    - a
    - a
- type: "ok"
  children: ["c1", "c2", a, a, a, a]

@mikefarah
Copy link
Owner

Thanks for raising with a clear example, definitely a bug, looking in to it.

mikefarah added a commit that referenced this issue Mar 19, 2022
@mikefarah
Copy link
Owner

Fixed in 4.23.1

@crisbal
Copy link
Author

crisbal commented Mar 20, 2022

Thanks for the quick fix! Really appreciate it. I will give it a try on monday.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants