0

I am trying to parse a JSON object and am having a difficult time accessing a value in a way that I can then merge with another df. The path for the value is: QueryResponse.Purchase[0].Line[0].Amount

This spec will give me the first value:

glom(data, ('QueryResponse.Purchase', ['Line'], (['0'], ['Amount'])))

but not the Amounts contained in the other Lines. I can access the other Amount values, but then have not been able to figure out how to merge that with my df containing values from the Purchase level

Barmar
  • 741,623
  • 53
  • 500
  • 612
Tom Woods
  • 19
  • 2

2 Answers2

1

Based on Glom Tutorial, you need to specify the "data" before using it in a glom methode.

Example (from there site):

from glom import glom

# Basic deep get

data = {'a': {'b': {'c': 'd'}}}

print(glom(data, 'a.b.c'))

In your case, you'll also need to import Json at the top (but you probably already did that).

Mathis Côté
  • 156
  • 1
  • 5
1

Without a data example, it's a little hard to assist with the issue.

However, assuming your data looks something like this:

data = {"QueryResponse": 
          {"Purchase": [
             {"Line": {"Amount": 10}}, 
             {"Line": {"Amount": 20}}
            ]
          }
        }

Then the query to get all of the Amount values from each Line entry puts the full path to the desired value in the list.

>>> glom(data, ('QueryResponse.Purchase', ['Line.Amount']))
[10, 20]
Joseph Cottam
  • 838
  • 7
  • 11