Questions tagged [glom]

glom is a Python module for declaratively accessing and transforming data

Features

  • Path-based access for nested structures
  • Declarative data transformation using lightweight, Pythonic specifications
  • Readable, meaningful error messages
  • Built-in data exploration and debugging features
  • Command-line interface (CLI) available

Links

33 questions
6
votes
2 answers

How do I filter a list in glom based on list index?

I want to be able to filter out just 1, or perhaps all list items less than a certain index with glom, but the filter snippets in the snippet section of the glom documentation doesn't show me how to do that. Example (keep just the 2 first items in a…
Ulrik
  • 493
  • 2
  • 16
5
votes
1 answer

Is there a way to merge a list of dicts into a single dict using glom library?

I'm using glom project. Is there a way to convert [{"id": 1, "name": "foo"}, {"id": 2, "name": "bar"}] to {1: "foo", 2: "bar"}?
proofit404
  • 281
  • 1
  • 13
5
votes
1 answer

Get value of nested attribute by filtering list on other attribute with Python Glom

I have a data object like this: data = { 'props': { 'items': [ {'name': 'a', 'content': 'plain'}, {'name': 'b', 'content': {'id': 'x'}}, {'name': 'c', 'content': {'id': 'y'}}, ] } } Using…
Pablo
  • 983
  • 10
  • 24
4
votes
2 answers

Nested dicts and lists / glom lib python

I am trying to access deep-nested lists and dictionaries. I am experimenting with the glom library however my Third_KV key doesn't work on the below JSON object when trying to retrieve the "Country" from glom import glom target = { "Result": { …
Fisqkuz
  • 99
  • 7
4
votes
1 answer

Slicing in glom?

In short: How can I slice in glom? That is, being able to specify that I want to extract from several indices (e.g. y = x[(2, 5, 11)]), not just a single one (e.g. y = x[2]). Simple example: Say I have this data: d = {'_result': [[1, 'a'], [2, 'b'],…
thorwhalen
  • 1,920
  • 14
  • 26
3
votes
1 answer

Glom handling or skipping a PathError for data with different keys?

I am using the glom package to help with traversing a big dictionary. Given this data: data = { 'groups': [ {'instance': {'name': 'football'}}, {'instance': {'name': 'rugby'}}, {'id': 145, 'type': 'unknown'}, ] } And…
tread
  • 10,133
  • 17
  • 95
  • 170
3
votes
1 answer

How to make glom work with dict-like objects

Glom (https://glom.readthedocs.io/en/latest/) is, amongst other things, for path-based access for nested structures But how do you make it work for nested structures beyond dicts? Consider the following class (purposely not an actual…
thorwhalen
  • 1,920
  • 14
  • 26
3
votes
1 answer

Extracting fields of a list of dictionaries into a new dictionary using glom

I have the following highly simplified structure elements = [{"id": "1", "counts": [1, 2, 3]}, {"id": "2", "counts": [4, 5, 6]}] I'd like to be able to construct, using glom, a new dictionary of the form {: }, e.g. for…
Ignacio Vergara Kausel
  • 5,521
  • 4
  • 31
  • 41
3
votes
1 answer

Using Python module Glom, Extract Irregular Nested Lists into a Flattened List of Dictionaries

Glom makes accessing complex nested data structures easier. https://github.com/mahmoud/glom Given the following toy data structure: target = [ { 'user_id': 198, 'id': 504508, 'first_name':…
Liquidgenius
  • 639
  • 5
  • 17
  • 32
2
votes
2 answers

Pythonic way to transform/flatten JSON containing nested table-as-list-of-dicts structures

Suppose I have a table represented in JSON as a list of dicts, where the keys of each item are the same: J = [ { "symbol": "ETHBTC", "name": "Ethereum", : }, { "symbol": "LTC", "name": "LiteCoin" …
P i
  • 29,020
  • 36
  • 159
  • 267
2
votes
2 answers

Get first item from nested list in glom

from glom import glom, T target = { "items": [ { "label": "valuation", "value": [ "900 USD" ] },] } spec =…
Lone Ronin
  • 2,530
  • 1
  • 19
  • 31
2
votes
0 answers

Glom assign using a Check filter

I'm trying to use the glom.assign operator to deeply assign and only set on one of the children in a list. As a complication, the correct child to set depends on a property of that child. Can I use assign and Check to assign some children's…
Joseph Cottam
  • 838
  • 7
  • 11
2
votes
1 answer

Create partial dict from recursively nested field list

After parsing a URL parameter for partial responses, e.g. ?fields=name,id,another(name,id),date, I'm getting back an arbitrarily nested list of strings, representing the individual keys of a nested JSON object: ['name', 'id', ['another', ['name',…
rypel
  • 4,686
  • 2
  • 25
  • 36
2
votes
1 answer

Looking for the likely better way to get at nested data with glom?

I have a particularly nasty stats object from a system that I need to retrieve data from (two of the many stats entries shown for brevity). 'https://localhost/mgmt/tm/sys/performance/all-stats/TMM%20Memory%20Used': {'nestedStats': {'entries':…
Jason Rahm
  • 670
  • 3
  • 14
2
votes
1 answer

Glom: How to include field-specific defaults

How can one include field-specific defaults in glom's output? For example, I can include "constants" like this: from glom import Spec, Literal extract = Spec({'A': 'a', 'B': Literal(42)}).glom assert extract({'a': 'life'}) == {'A': 'life', 'B':…
thorwhalen
  • 1,920
  • 14
  • 26
1
2 3