mahmoud / glom

☄️ Python's nested data operator (and CLI), for all your declarative restructuring needs. Got data? Glom it! ☄️
https://glom.readthedocs.io
Other
1.91k stars 63 forks source link

Q about conditional filtering #176

Open joncooper opened 4 years ago

joncooper commented 4 years ago

glom is awesome. Thank you very much for it.

I'm not sure if this is the right place for Q&A but figured I'd give this a shot. If there's a more appropriate place to ask, please let me know and I'll close and move to that venue.

Simplifying a bit, I have data that looks like this:

[
   { 
     'id': 1,
     'people': [
         { 'type': 'PRIMARY', 'name': {'firstName': 'Alice', lastName': 'Example' } },
         { 'type': 'SECONDARY', 'name': {'firstName': 'Bob', 'lastName': 'Example' } }
      ] 
   },
   {
      'id': 2,
      'people': [
         { 'type': 'PRIMARY', 'name': {'firstName': 'Cindy', 'lastName': 'Example' } }
      ]
   }
]

I'd like to glom out into the following representation:

[
   {
      'id': 1,
      'primary.firstName': 'Alice',
      'primary.lastName': 'Example',
      'secondary.firstName': 'Bob',
      'secondary.lastName': 'Example'
   },
   {
      'id': 2,
      'primary.firstName': 'Cindy',
      'primary.lastName': 'Example'
   }
]

It seems to me that this is certainly possible but I haven't been able to figure out how to filter on the value corresponding to the 'type' key.

Would be grateful for any help figuring this out. I can certainly do it in a less pretty way but I'm sure the glom solution is elegant.

Thanks, Jon