jmespath / jmespath/jmespath.site

Example on clearing None objects in lists

Open
#124 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Makefile
Stars
61
Forks
61
PR merge metrics
No merged PRs in 30d

Description

I haven an example where I want to extract a property from an object that could or could not be in 2 different fields hence it's an OR behavior.

So here are 3 possible values for this object:
{"first_level": {"second_level": {"first_place": {"property": "aimed_value"}, "second_place": {"property": "another_aimed_value"}}}}
{"first_level": {"second_level": {"first_place": {"property": "aimed_value"}}}}
{"first_level": {"second_level": {"second_place": {"property": "another_aimed_value"}}}}

Found this expression: "first_level.second_level.[first_place.property,second_place.property]"

And these are the possible results:
["aimed_value", "another_aimed_value"]
["aimed_value", None]
[None, "another_aimed_value"]

I didn't want this and didn't find any examples for cleaning this data, but fooling around I realized that adding [] at the end of the expression removes the None values from the results. This would be great to have displayed on the examples section.

So this is the final expression: "first_level.second_level.[first_place.property,second_place.property][]"

And these are the final results:
["aimed_value", "another_aimed_value"]
["aimed_value"]
["another_aimed_value"]

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.