Reputation: 2784
I am using the simple_salesforce library for importing data from Salesforce. 2 rows come back as what looks like a triple nested dict..
OrderedDict([(u'totalSize', 2), (u'done', True), (u'records',
[OrderedDict([(u'attributes',
OrderedDict([(u'type', u'Account'), (u'url',
u'/services/data/v29.0/sobjects/Account/98234fd')])), (u'Name',
u'Adidas'), (u'Client_ID', u'100')]),
OrderedDict([(u'attributes', OrderedDict([(u'type', u'Account'),
(u'url', u'/services/data/v29.0/sobjects/Account/43243sd')])),
(u'Name', u'Nike'), (u'Client_ID', u'101')])
])
])
I tried queryResults.items()[2].items()[1]
but it looks like you can't nest the ordered dict calls.
How do I get a 2 dimensional (nested) list that would return the 'Name' and 'Client_ID' columns?
Upvotes: 2
Views: 166
Reputation: 2577
If I got your answer right this should do the trick:
from collections import OrderedDict
d = OrderedDict([(u'totalSize', 2), (u'done', True), (u'records', [
OrderedDict([(u'attributes',
OrderedDict([(u'type', u'Account'), (u'url',
u'/services/data/v29.0/sobjects/Account/98234fd')])), (u'Name',
u'Adidas'), (u'Client_ID', u'100')]),
OrderedDict([(u'attributes', OrderedDict([(u'type', u'Account'),
(u'url', u'/services/data/v29.0/sobjects/Account/43243sd')])),
(u'Name', u'Nike'), (u'Client_ID', u'101')])
])
])
data = [(e["Name"], e["Client_ID"]) for e in d["records"]]
print(data)
[("Adidas", "100"), ("Nike", "101")]
Upvotes: 4
Reputation: 1527
queryResults.items()[2][1]
That gives you a list of OrderedDict
's with the attributes. Thus:
data = []
for i in queryResults.items()[2][1]:
name = i['Name']
clientID = i['Client_ID']
data.append([name,clientID])
for i in data:
print(i[0] + " " + i[1]) # This shows you what is is data.
Upvotes: 1