Skip to content

Assigning empty to multiple paths #2440

@johannes-riecken

Description

@johannes-riecken

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions