Skip to content

walk function not applied to child object of match #1724

@cwingrav

Description

@cwingrav

Describe the bug
The walk function says it, "applies f recursively to every component of the input entity." However, it does not seem to apply to an inner object, after a match has been made. i.e. a child object will not be matched if its parent matches.

To Reproduce
In my example, I am pulling data from a child object, into the parent object. This works just fine for the 'a.MATCH.c' case below, but fails for 'a.inner.MATCH.b'. Possibly a documentation issue (in that case, how do I recursively match?), a bug or is my code pulling into an object that gets removed?

CODE:

jq '. * walk( if type == "object" and has("MATCH") then .|=.MATCH else . end) ' a.json

INPUT:

{
  "a" : {
    "inner" : {
      "MATCH" : {
        "b" : 1
      } 
    },
    "MATCH" : {
      "c" : 2
    } 
  }
}

OUTPUT:

The a.MATCH.c is brought out to a.c but a.inner.MATCH.b is not brought out into a.inner.b.

{
  "a": {
    "inner": {
      "MATCH": {
        "b": 1
      }
    },
    "MATCH": {
      "c": 2
    },
    "c": 2
  }
}

Expected behavior

EXPECTED:
I would expect a.inner.b to exist.

{
  "a" : {
    "inner" : {
      "MATCH" : {
        "b" : 1
      },
      "b" : 1
    },
    "MATCH" : {
      "c" : 2
    },
    "c" : 2
  }
}

Environment (please complete the following information):

  • macOS - 10.13.6
  • jq version - jq-HEAD-8eff744

Additional context
Add any other context about the problem here.

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