jq icon indicating copy to clipboard operation
jq copied to clipboard

Assigning empty to multiple paths

Open johannes-riecken opened this issue 3 years ago • 1 comments

Describe the bug

In (.[].children|.[])|=if has("color") then . else empty end, empty behaves different from regular values.

To Reproduce

Run jq '(.[].children|.[])|=if has("color") then . else empty end' foo.json, where foo.json is:

[
    {
        "name": "foo",
        "children": [{
            "name": "foo.0",
            "color": "red"
        }]
    },
    {
        "name": "bar",
        "children": [{
            "name": "bar.0",
            "color": "green"
        },
        {
            "name": "bar.1"
        }]
    },
    {
        "name": "baz",
        "children": [{
            "name": "baz.0"
        },
        {
            "name": "baz.1"
        }]
    }
]

Output:

[
  {
    "name": "foo",
    "children": [
      {
        "name": "foo.0",
        "color": "red"
      }
    ]
  },
  {
    "name": "bar",
    "children": [
      {
        "name": "bar.0",
        "color": "green"
      }
    ]
  },
  {
    "name": "baz",
    "children": [
      {
        "name": "baz.1"
      }
    ]
  }
]

Expected behavior

I expected this output, which I can get by running del(.[].children[] | select(has("color") | not)).

[
  {
    "name": "foo",
    "children": [
      {
        "name": "foo.0",
        "color": "red"
      }
    ]
  },
  {
    "name": "bar",
    "children": [
      {
        "name": "bar.0",
        "color": "green"
      }
    ]
  },
  {
    "name": "baz",
    "children": []
  }
]

Environment (please complete the following information):

  • OS and Version: macOS Monterey (M1 architecture)
  • jq version 1.6

johannes-riecken avatar Jun 09 '22 17:06 johannes-riecken

This is duplicate of #2051.

itchyny avatar Jun 13 '22 04:06 itchyny