How to build array from json input file in python 3? [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am trying to build an array from the information comming from a local json file.
The desired output array format is:
output = [
'elementa-element2-element3',
'elementa-element2-element3',
]
The input from json is:
import json
with open('/Users/user/test.json', 'r') as f:
array = json.load(f)
print (array)
{'responseHeader': {'zkConnected': True, 'status': 0, 'QTime': 2, 'params': {'q': 'facet_gender:man', 'start': '0', 'fq': 'lang:de', 'rows': '10000', 'wt': 'json', 'facet': 'true'}}, 'response': {'numFound': 1005, 'start': 0, 'docs': [
{'rmc': 'm228238-0042', 'title': 'Day-Date 40', 'family': 'Day-Date', 'familyCode': 'day-date', 'facet_case_title': ['Oyster, 40 mm, Gelbgold']},
{'rmc': 'm326935-0007', 'title': 'Sky-Dweller', 'family': 'Sky-Dweller', 'familyCode': 'sky-dweller', 'facet_case_title': ['Oyster, 42 mm, Everose-Gold']}, ...
How can I acess the rows and build my array from all rows?
output = array
while i < len(array)
output[i] = array['response']['docs'][i]['familyCode']+'/'+array['response']['docs'][i]['rmc']

The json.load() will return a dictionary. This dictionary can be nested, if the json file contains nested elements. This is the case in your example, too.
If you format your json a little bit, it will be more clear:
{
'responseHeader': {
'zkConnected': True,
'status': 0,
'QTime': 2,
'params': {
'q': 'facet_gender:man',
'start': '0',
'fq': 'lang:de',
'rows': '10000',
'wt': 'json',
'facet':
'true'
}
},
'response': {
'numFound': 1005,
'start': 0,
'docs': [
{'rmc': 'm228238-0042', 'title': 'Day-Date 40', 'family': 'Day-Date', 'familyCode': 'day-date', 'facet_case_title': ['Oyster, 40 mm, Gelbgold']},
{'rmc': 'm326935-0007', 'title': 'Sky-Dweller', 'family': 'Sky-Dweller', 'familyCode': 'sky-dweller', 'facet_case_title': ['Oyster, 42 mm, Everose-Gold']},
...
]
}
}
If you want to retrieve the "docs", you actually need this:
array["response"]["docs"]
In your example, it is a list, and it can be used as you would normally do.
Example 1: get the first element of the list
documents = array["response"]["docs"] # Create new variable "documents".
print(documents[0]) # Print out first element of "docs" variable.
Example 2: Iterate over every element:
documents = array["response"]["docs"]
for document in documents:
print(document)

Related

Editing Dictionary in Python Not Working in Function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 days ago.
Improve this question
I'm trying to edit a dictionary. My logic works without the use of a function, but once I add a function to the mix, it doesn't.
The results of the following code are what I desire.
thDic = {
'123': {
'type': 'Web Map'
},
'456': {
'type': 'Web Mapping Application'
}
}
for k,v in thDic.items():
v['type'] = 'asdf'
thDic
{'123': {'type': 'asdf'}, '456': {'type': 'asdf'}}
However, when I put the for loop into a function, it stops giving me the desired results.
def test(thDic):
for k,v in thDic.items():
v['type'] = 'asdf'
return thDic
thDic
{'123': {'type': 'Web Map'}, '456': {'type': 'Web Mapping Application'}}
You did not call the function
def test(thDic):
for k,v in thDic.items():
v['type'] = 'asdf'
return thDic
thDic = test(thDic)
thDic

How to convert dict containing tuple values to string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 10 months ago.
Improve this question
How to convert the following Input to the desired output using python.
Input:
{'Id': (34,), 'user': ('a.t#gmail.com',), 'createdOn': ('12 Oct',), 'status': (False,), 'message': ('Hello',)}
Output:
{'Id': 34, 'user': 'a.t#gmail.com', 'createdOn':'12 Oct', 'status': False, 'message': 'Hello'}
Just take first index for each value :
your_dict = {'Id': (34,), 'user': ('a.t#gmail.com',), 'createdOn': ('12 Oct',), 'status': (False,), 'message': ('Hello',)}
your_dict = {key:val[0] for key, val in your_dict.items()}
Why are the values in a tuple to begin with?
old_dict = {'Id': (34,), 'user': ('a.t#gmail.com',), 'createdOn': ('12 Oct',), 'status': (False,), 'message': ('Hello',)}
new_dict = {k:v[0] for k,v in old_dict.items()}
print(new_dict)

Extracting data from nested JSON using python

I am hoping someone can help me solve this problem I am having with a nested JSON response. I have been trying to crack this for a few weeks now with no success.
Using a sites API I am trying to create a dictionary which can hold three pieces of information, for each user, extracted from the JSON responses. The first JSON response holds the users uid and crmid that I require.
The API comes back with a large JSON response, with an object for each account. An extract of this for a single account can be seen below:
{
'uid': 10,
'key':
'[
N#839374',
'customerUid': 11,
'selfPaid': True,
'billCycleAllocationMethodUid': 1,
'stmtPaidForAccount': False,
'accountInvoiceDeliveryMethodUid': 1,
'payerAccountUid': 0,
'countryCode': None,
'currencyCode': 'GBP',
'languageCode': 'en',
'customFields':
{
'field':
[{
'name': 'CRMID',
'value': '11001'
}
]
},
'consentDetails': [],
'href': '/accounts/10'}
I have made a loop which extracts each UID for each account:
get_accounts = requests.get('https://website.com/api/v1/accounts?access_token=' + auth_key)
all_account_info = get_accounts.json()
account_info = all_account_info['resource']
account_information = {}
for account in account_info:
account_uid = account['uid']
I am now trying to extract the CRMID value, in this case '11001': {'name': 'CRMID', 'value': '11001'}.
I have been struggling all week to make this work, I have two problems:
I would like to extract the UID (which I have done) and the CRMID from the deeply nested 'customFields' dictionary in the JSON response. I manage to get as far as ['key'][0], but I am not sure how to access the next dictionary that is nested in the list.
I would like to store this information in a dictionary in the format below:
{'accounts': [{'uid': 10, 'crmid': 11001, 'amount': ['bill': 4027]}{'uid': 11, 'crmid': 11002, 'amount': ['bill': 1054]}]}
(The 'bill' information is going to come from a separate JSON response.)
My problem is, with every loop I design the dictionary seems to only hold one account/the last account it loops over. I cant figure out a way to append to the dictionary instead of overwrite whilst using a loop. If anyone has a useful link on how to do this it would be much appreciated.
My end goal is to have a single dictionary which holds the three pieces of information for each account (uid, crmid, bill). I'm then going to export this into a CSV document.
Any help, guidance, useful links etc would be much appreciated.
In regards to question 1, it may be helpful to print each level as you go down, then try and work out how to access the object you are returned at that level. If it is an array it will using number notation like [0] and if it is a dictionary it will use key notation like ['key']
Regarding question 2, your dictionary needs unique keys. You are probably looping over and replacing the whole thing each time.
The final structure you suggest is a bit off, imagine it as:
accounts: {
'10': {
'uid': '10',
'crmid': 11001,
'amount': {
'bill': 4027
}
},
'11': {
'uid': '11',
'crmid': 11011,
'amount': {
'bill': 4028
}
}
}
etc.
So you can access accounts['10']['crmid'] or accounts['10']['amount']['bill'] for example.

Python: TypeError: string indices must be integers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
This is a similar problem posted here lots of time but I am unable to get my head around it.
import json
str2 ="""[{'cta': [], 'ctr': 2880509, 'client_id': '229132', 'exchange': 'NSE_EQ', 'token': '3063', 'product': 'CO', 'order_type': 'M', 'duration': 'DAY', 'price': '0', 'trigger_price': '149.10', 'quantity': 1, 'disclosed_quantity': 0, 'side': 'S', 'avg_price': '148.10', 'traded_quantity': 1, 'pending_quantity': 0, 'message': '', 'exchange_order_id': '1300000006005800', 'syom_order_id': 'NA', 'order_number': '191101000336718', 'timestamp': '01/11/2019,12:19:45', 'exchange_timestamp': '01-Nov-2019 12:19:45', 'status': 'complete', 'time_in_micro': '1572590985928000', 'is_amo': False, 'order_complexity': 'CO', 'request_id': '1', 'valid_date': '--', 'tag': 'JWEB|TB1', 'comments': 'PLACE ORDER :: 229132|NSE_EQ|3063|EQ|I|0|1|S|CO|WEB|IP-172-31-4-125|1572590985897', 'fill_id': '', 'original_message': '', '_amo': False}]"""
str2 = (json.dumps(str2))
print(str2)
print(str2['client_id'])
Why this doesn't work? It says -
print(str2['client_id'])
TypeError: string indices must be integers
Note that str2 is output from someplace and my goal is to fetch the client_id or any other variable.
So what I am seeking is what is the possible way to parse it?
There are mutliple problems here. You don't seem to be thinking this through.
You start with a string. Calling json.dumps on a string just gives you another string. But you couldn't have called json.loads on the original string either, because it is not JSON; it appears to be a string representation of a Python object.
But then, even if you had correctly parsed it, it still wouldn't work, because it represents a list of dictionaries, not a single dict.
It seems unlikely that this is actually the output from your external system. If you want further help, you will need to explain exactly how you got that string.

Python 2.7: Removing a dict in a list if a key is missing or empty

The list in question looks something like this, just a list of Blogs with any Posts if applicable:
blogs = {
{
'id': 1,
'title': 'Foodies',
'posts': {
{ 'id': 28, 'title': 'Sourdough Bread starter', 'blog_id': 1},
{ 'id': 64, 'title': 'How to make brioche in under 4 hours', 'blog_id': 1}
}
},{
'id': 2,
'title': 'Southern Meals',
'posts': {}
},{
'id': 3,
'title': 'Vegomamma'
},{
'id': 4,
'title': 'Culinare'
}
}
I only want the Blogs with Posts, so I'm trying to reduce the list so that I would only get the first dict returned.
Here's what I tried, which threw the error:
"'dict' object has no attribute 'posts'"
Which I understand, but I am trying to remove dicts without that attribute.
for b in blogs:
if "posts" not in b or b.posts.count() == 0:
blogs.remove(b)
Why does this fail? It seemed like a pretty simple solution, and I've used it before.
This app is built in Python and Angular, so I could do the filtering in Angular, but I'd rather take care of it in Python.
EDIT Added the exact error message.
First off all your data structure is not a valid python object. You have a set contain dictionaries that are not hashable objects (will raise TypeError). Based on your question body It seems that it's a list. Secondly, you don't need to check the existence of posts within the dictionary you can jsut use dict.get() within a list comprehension. get method returns None if the key is missing:
In [20]: [b for b in blogs if b.get('posts')]
Out[20]:
[{'posts': [{'id': 28, 'title': 'Sourdough Bread starter', 'blog_id': 1},
{'id': 64, 'title': 'How to make brioche in under 4 hours', 'blog_id': 1}],
'id': 1,
'title': 'Foodies'}]
Also, note that since the post is supposed to be an iterable the validation check will failed if it's empty (it evaluated as False). That's why I just used if b.get('posts').

Categories

Resources