2 ms·
I would say, that being a DSL is a benefit by itself. This is how the above example would look in Python: [character['name'] for book in library for charac
by krick 11y ago
I would say, that being a DSL is a benefit by itself. This is how the above example would look in Python:
[character['name'] for book in library for character in book['mainCharacters']]
(Python also has maps and stuff, but it's considered non-idiomatic, plus flatten would be more awkward.)
First of all, syntax is quite different from what you use (Scala, I suppose?), when were we using JSONPath we could just copy 1 line from one project to another and that would be fine.
Moreover, both our implementations have the same problems: we assume every book has mainCharacters and every character has name. Would it be JSONPath — it doesn't matter, no 'mainCharacters' means path doesn't match the pattern, just skip it. In our cases this means exceptions.
And what if we want to get all 'name' fields from whatever object at whatever depth? Or 'name' of every object where 'color' is 'yellow'?
Now, if you consider dictionary structure much more nested (say, some AST) — processing that without errors would be quite painful. And you also would end up writing your own XPath (JSONPath), even with all your map's and reduce's.
Of course, should it get complicated enough we would end up needing to write something custom anyway, but stuff like JSONPath just helps to keep things simple when possible. That's it.