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

walk function not applied to child object of match #1724

Closed
cwingrav opened this issue Sep 24, 2018 · 1 comment
Closed

walk function not applied to child object of match #1724

cwingrav opened this issue Sep 24, 2018 · 1 comment
Labels

Comments

@cwingrav
Copy link

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.

@pkoppstein
Copy link
Contributor

pkoppstein commented Sep 24, 2018

I believe the problem is a mismatch between your code and your expectations.

Anyway, the result you've indicate you expect can be achieved using the following invocation of walk:

walk( if type == "object" and has("MATCH") then .+.MATCH else . end) 

By the way, if you look at the code for def walk(_):, I think you'll agree the description that you mention is fair, but it could perhaps be improved because of what some might say is an ambiguity, as suggested by the following variant of walk:

def postwalk(f):
  . as $in
  | if type == "object" then
      reduce keys_unsorted[] as $key
        ( {}; . + { ($key):  ($in[$key] | f) | postwalk(f) } ) 
  elif type == "array" then map( f ) | postwalk(f)
  else f
  end;

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

3 participants