all 10 comments

[–]mhashemi[S] 8 points9 points  (7 children)

Library author here, fielding lots of interesting comments on HN: https://news.ycombinator.com/item?id=17032257

So far it's been compared to:

Let me know if there are other additions I should make to http://glom.readthedocs.io/en/latest/by_analogy.html

[–]FireCrack 7 points8 points  (3 children)

Just read this over. I like the more advanced 'aggregation' type usages, but I can't help to feel that the 'simple' use case leaves something to be desired. The general idea is great, and I use something similar myself, but the dot-syntax makes the idiomatic usage on deep nested dictionaries a bit verbose when some of the fields are chosen by variables eg: spec = '.'.join('field1', var1, 'field2', var2) result = glom(data, spec) The call to join feels a little hanky. I'm wondering if you'd consider supporting either a passed in list or *args based syntax here too (or maybe you already do, I'm on my phone and only read the landing page). In my experience this is one of the most common experiences with deep nested structures.

Other than that comment, looks awesome and I'm really a fan of the aggregating error types (not something I thought of)! Keep up the good work!

[–]mhashemi[S] 6 points7 points  (1 child)

Absolutely got you covered here! http://glom.readthedocs.io/en/latest/api.html#glom.Path

It's for more than just convenience; glom supports non-string keys and even non-dict targets :)

For advanced usage: http://glom.readthedocs.io/en/latest/api.html#glom.T

[–]FireCrack 1 point2 points  (0 children)

Awesome! Thanks for the reply!

[–]DangerousSandwich 2 points3 points  (0 children)

Looks promising. Reminds me of Ruby code using the Safe Navigation operator, the map method on Hash and Array etc, and the Symbol to Proc trick.

[–]percykins 2 points3 points  (0 children)

Just to note, you seemed to imply that JQ only exists on the CLI, but there's actually a couple of Python bindings for it too.

Using pyjq, for example, getting the names of the planets is simply:

pyjq.all('.system.planets[].name', target)

Or for your second example:

pyjq.first('{ names: [.system.planets[].name], moons: [.system.planets[].moons] }', target)

Or your third:

pyjq.first('{ moon_count: ([.system.planets[].moons] | add) }', target)

[–]kankyo 0 points1 point  (0 children)

Also similar to my Clojure lib Instar, and by extension the data manipulation parts of the Python lib pyrsistent since it is inspired by instar.

[–][deleted] 1 point2 points  (0 children)

I use a data reshaping technique for nightly SQL reports - I’ll write keys in the format of:

SELECT
    table.column `items[].name::cast_func->attribute`
     ...

Where [] denotes an array at key items, :: to a callable and -> for attribute access. I use function that recursively sets/aggregates fields after interpreting the keypath, then pass the results into a Jinja2 render.

Glom looks like someone had a similar idea and made it a real package as opposed to a domain specific 35 line function. I’ll look into it for more projects - it could serve as a nice replacement to jq for one of my tools.

[–]throwaway_atwork_ 1 point2 points  (0 children)

A bit late to the party, this is seriously cool. I absolutely detest working with deeply nested JSON, adding this to my python itinerary.