Giter VIP home page Giter VIP logo

Comments (7)

stedolan avatar stedolan commented on July 4, 2024 6

If you build from master, you can now do this:

del(.bar)

It's not documented yet, and the exact semantics of what happens when you replace .bar with something more complicated will probably change, but that should solve the problem.

from jq.

stedolan avatar stedolan commented on July 4, 2024 3

Ah. You can in fact do this. First you need a means to recurse down to all of the children of a node, which you can do by defining a recursive function:

def all_children: . , (.data.children | select(. != null) | .[] | all_children);

Putting a node through all_children returns the node itself and all child nodes. Then, to get all of the author fields from any node in your dataset, you'd need:

.[] | all_children | .author

You can then delete all of the author fields with:

del(.[] | all_children | .author)

Defining a new recursive function each time you want to do this is effort, so I've added a recurse function to the standard library, which is defined as:

def recurse(f): . , (f | select(. != null) | recurse(f));

The program to delete all author fields anywhere in the document now becomes:

del(.[] | recurse((.data.children // []) | .[]) | .author)

(foo // [] returns [] if foo is null, or foo otherwise.)

from jq.

stedolan avatar stedolan commented on July 4, 2024 2

Now I'm happier with what del(complicated_thing) does. You can del() all sorts of junk:

  • del(.foo,.bar,.baz) deletes those three keys
  • del(.foo[0].bar.baz) removes the baz key from the object at .foo[0].bar
  • del(.foo[0,1,2]) deletes the first three items of the array at field "foo"
  • del(.foo[] | select(.score < 20)) deletes items in the array at field "foo" whose "score" field is below 20.

Not quite sure what you meant by "recursive", though.

from jq.

stedolan avatar stedolan commented on July 4, 2024

Yes, this is definitely a missing feature. Options include:

  1. . - "bar"
  2. . - ["bar"]
  3. remove("bar")
  4. .bar = empty

I think 1 or 2 might be what you suggested. I'm not sure which I prefer yet, but I'm kind of partial towards 4 since it fits with the current meanings of assignment and empty, rather than adding a new construct.

from jq.

paul-hammant avatar paul-hammant commented on July 4, 2024

you're the domain expert, choose the most appropriate solution :)

from jq.

paul-hammant avatar paul-hammant commented on July 4, 2024

That's great dude. Is it recursive too ?

from jq.

paul-hammant avatar paul-hammant commented on July 4, 2024

By recursive, I mean that it's able to remove a node that repeats throughout a document structure. For example, Reddit's articles go fairly deep with repeating element/attr names:

from jq.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.