How to parse a JSON object? [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I know there are a lot of questions like these but I am not able to find an answer to my question. Or probably, I am missing something here. The format of my JSON is:
{
"fields": [
{
"type": "long",
"name": "UniqId",
"data": "True"
},
{
"type": "string",
"name": "Name",
"data": "True"
},
{
"type": "string",
"name": "Address",
"data": "False"
}
],
"type": "struct"
}
I just want name and type to be extracted and appended to a list. First element to be name and second to be type.
The above format is saved in a variable json_dump. And when I do:
for k in json_dump1.iteritems():
print k
It gives me an error
"AttributeError: 'str' object has no attribute 'iteritems".
I would really appreciate the help. Thanks.

json_dump1 is a string, so you need to parse it into an object first. This can be done with the json library:
import json
parsed_json = json.loads(json_dump1)
It sounds like you also want to do some comprehension on this. It's unclear exactly how you want the name and type values to be stored, but you would want something like this to iterate over parsed_json:
results = []
for f in parsed_json['fields']:
results.append(f['name'])
results.append(f['type'])

For cases like this, it's better to think of the json as a dictionary containing nested lists or dictionaries within them. So if you treat them as such and iterate over the lists/keys you should be fine. In this case, the first key is fields, after that we have a number of lists that each contain a dictionary, so we iterate over each of these lists and extract the desired keys (name and type):
name_list = []
type_list = []
for i in json_file['fields']:
name_list.append(i['name'])
type_list.append(i['type'])
print name_list
print type_list
Output:
['UniqId', 'Name', 'Address']
['long', 'string', 'string']
You can also create a dictionary in which you will append these values:
json_values = {'name':type_name,'list':type_list}
Of course you can define this first and append the values to the lists in the dictionaries:
json_values = {'name':[],'list':[]
name_list = []
type_list = []
for i in json_file['fields']:
json_values['name'].append(i['name'])
json_values['list'].append(i['type'])

Related

Navigating JSON with variable keys in Python? [duplicate]

This question already has answers here:
Use Variable As Dictionary Key Set
(2 answers)
How to use a dot "." to access members of dictionary?
(36 answers)
Closed last month.
Lets say I have some json like so store in a variable called data
{
"print": {
"ams": { "exists": 1},
"fan_speed": 29,
"reports": [
{"name": "foo"},
{"name": "bar"}
]
}
}
Now I've got a variable which is the key i want to return stored in a variable called key for example print.fan_speed, print.ams.exists, print.reports[0].name
What I want to is something like data.get(key). What is the best way to approach this?
The following should work its way into your data, including indexing into lists:
import re
data = {
"print": {
"ams": { "exists": 1},
"fan_speed": 29,
"reports": [
{"name": "foo"},
{"name": "bar"}
]
}
}
def value_of(data, location):
for part in location.split("."):
match = re.match(r"(.*)\[(\d+)\]$", part)
if match:
name, index = match.groups()
data = data.get(name)[int(index)]
else:
data = data.get(part)
if not data:
return None
return data
print(value_of(data, "print.ams.exists"))
print(value_of(data, "print.reports[1].name"))
Result:
1
bar
It could do with a little rationalisation as it will return None for a non-existent key, but will error on a bad index - it should do one or the other depending on your requirements but the concept is there.
The concept is to take each '.' separated element of the string in turn, using it to dig further into the data structure. If the element matches the syntax of 'name[index]' using the regex, the component is treated as a list and the indexth element is extracted.

Python search and replace whilst caching [duplicate]

This question already has answers here:
How can I make a dictionary (dict) from separate lists of keys and values?
(21 answers)
Closed 4 months ago.
I'm attempting to search and replace using information from 2 lists, this is whilst caching any replacements that have been done so the same corresponding values can be given.
For example, I have the following -
names = ["Mark","Steve","Mark","Chrome","192.168.0.1","Mark","Chrome","192.168.0.1","192.168.0.2"]
type = ["user","user","user","process","address","user","process","adress","address"]
And I'm hoping to get the following output -
{
"Mark":"user1",
"Steve":"user2",
"Chrome":"process1",
"192.168.0.1":"adress1",
"192.168.0.2":"adress2"
}
So trying to use the type in the the 2nd list to determine the item in the first list's corresponding value.
Hope this makes sense, is this possible? Any help would be appreciated.
I would recommend you use a dictionary personally.
names = {
"Mark": "user",
"Steve": "user2",
"Chrome": "process1",
"192.168.0.1": "address1",
"192.168.0.2": "address2"
}
print(names["Mark"])
By using this dictionary you can precisely tap into the name you'd like to information of or anything else you want. It is also a little more readable
To form a dictionary from said values you can iterate the range and access values with the same index:
output = {names[i]: types[i] for i in range(len(names))}
Also refrain from using variable name type because it's already taken by a builtin Python syntax.
Looks like you're also trying to store / retrieve the count of the types (i.e. "user1", "user2, "address1", etc.). Hence, we need another data structure to keep count of the types already registered in our "hashmap" (dictionary in python). In the below solution, we use the type_cache.
The code should work as is.
from collections import defaultdict
names = ["Mark", "Steve", "Mark", "Chrome", "192.168.0.1", "Mark", "Chrome", "192.168.0.1", "192.168.0.2"]
types = ["user", "user", "user", "process", "address", "user", "process", "address", "address"]
expected = {
"Mark": "user1",
"Steve": "user2",
"Chrome": "process1",
"192.168.0.1": "address1",
"192.168.0.2": "address2"
}
def process_names_and_types(names, types):
result = {}
type_cache = defaultdict(int)
for name, type_ in zip(names, types):
if name not in result:
type_cache[type_] += 1
result[name] = f"{type_}{type_cache[type_]}"
return result
if __name__ == "__main__":
actual = process_names_and_types(names, types)
assert actual == expected, f"Expected: {expected}, Actual: {actual}"

Iterate through a nested python dict

I have a JSON file that looks like this:
{
"returnCode": 200,
"message": "OK",
“people”: [
{
“details: {
"first": “joe”,
“last”: doe,
“id”: 1234567,
},
“otheDetails”: {
“employeeNum”: “0000111222”,
“res”: “USA”,
“address”: “123 main street”,
},
“moreDetails”: {
“family”: “yes”,
“siblings”: “no”,
“home”: “USA”,
},
},
{
“details: {
"first": “jane”,
“last”: doe,
“id”: 987654321,
},
“otheDetails”: {
“employeeNum”: “222333444”,
“res”: “UK”,
“address”: “321 nottingham dr”,
},
“moreDetails”: {
“family”: “yes”,
“siblings”: “yes”,
“home”: “UK,
},
}
This shows two entries, but really there are hundreds or more. I do not know the number of entries at the time the code is run.
My goal is to iterate through each entry and get the 'id' under "details". I load the JSON into a python dict named 'data' and am able to get the first 'id' by:
data['people'][0]['details']['id']
I can then get the second 'id' by incrementing the '0' to '1'. I know I can set i = 0 and then increment i, but since I do not know the number of entries, this does not work. Is there a better way?
Less pythonic then a list comprehension, but a simple for loop will work here.
You can first calculate the number of people in the people list and then loop over the list, pulling out each id at each iteration:
id_list = []
for i in range(len(data['people'])):
id_list.append(data['people'][i]['details']['id'])
You can use dict.get method in a list comprehension to avoid getting a KeyError on id. This way, you can fill dictionaries without ids with None:
ids = [dct['details'].get('id') for dct in data['people']]
If you still get KeyError, then that probably means some dcts in data['people'] don't have details key. In that case, it might be better to wrap this exercise in try/except. You may also want to identify which dcts don't have details key, which can be gathered using error_dct list (which you can uncomment out from below).
ids = []
#error_dct = []
for dct in data['people']:
try:
ids.append(dct['details']['id'])
except KeyError:
ids.append(None)
#error_dct.append(dct)
Output:
1234567
987654321

What is the best way to parse the following text in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to use the following text, and parse it to python, but can't find a good way for that
the content is in a text file, and I want to use the data in python.
what kind of type to use? I want to populate it to a menu.
"us-west-2": {
"focal-amd64": "ami-06e54d05255faf8f6",
"rhel7.5-x86_64": "ami-6f68cf0f",
"xenial-amd64": "ami-09b42c38b449cfa59",
"bionic-amd64": "ami-03804ed633fe58109",
"rhel8-x86_64": "ami-02f147dfb8be58a10"
},
"us-east-1": {
"oracle6.5": "ami-c034c5ad",
"rhel7.5-x86_64": "ami-0394fe9914b475c53",
"rhel7.7-x86_64": "ami-0916c408cb02e310b",
"trusty-amd64": "ami-0d859172c8670bbcd",
"xenial-amd64": "ami-028d6461780695a43",
"rhel7.6-x86_64": "ami-08a7d2bfef687328f",
"aws-ena": "ami-ccd280db",
"rhel8-x86_64": "ami-098f16afa9edf40be",
"rhel7.9-x86_64": "ami-005b7876121b7244d",
"bionic-amd64": "ami-07025b83b4379007e",
"centos6.9-x86_64": "ami-25491433",
"focal-amd64": "ami-0dba2cb6798deb6d8"
},
"us-east-2": {
"focal-amd64": "ami-07efac79022b86107",
"rhel7.9-x86_64": "ami-0d2bf41df19c4aac7",
},
"eu-central-1": {
"bionic-amd64": "ami-054e21e355db24124",
"xenial-amd64": "ami-05710338b6a5013d1",
},
"eu-west-1": {
"focal-amd64": "ami-06fd8a495a537da8b",
"rhel7.5-x86_64": "ami-02ace471",
"rhel7.6-x86_64": "ami-04c89a19fea29f1f0",
"rhel7.9-x86_64": "ami-020e14de09d1866b4",
"bionic-amd64": "ami-0727f3c2d4b0226d5",
"rhel8-x86_64": "ami-08f4717d06813bf00"
},
"ap-northeast-1": {
"bionic-amd64": "ami-003371bfa26192744",
"xenial-amd64": "ami-097beac0bacfefe65",
"focal-amd64": "ami-09b86f9709b3c33d4",
}
Your data is almost in json format: if you
add enclosing brackets {} around it, and remove any commas , before a closing
bracket (there are 3 of them), your data will be fully json-compliant.
Now assuming your data is in a file called foobar.json, the following code will
parse it into a python dictionary named d:
import json
with open('foobar.json', 'r') as f:
d = json.load(f)
You can now easily access and use your data, for example
print(d['us-west-2']['focal-amd64'])
will print ami-06e54d05255faf8f6.

Parse a string into Dictionary [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 8 years ago.
Improve this question
I have a string which is of the form dictionary for sure.
I need to parse it and store it as a dictionary in python.
What i have tried is this:
myObj={}
tmp=""
if ':' in line:
key,value = line.split(':')
key = key.strip('"')
value = value.lstrip(' ').rstrip(',')
if value == '{':
tmp += key + '.'
if value == '}':
tmp = ''
if(value!="{"):
myObj[tmp + key] = value
Reading Line by Line and parsing it. But I am facing problems with different kind of formats.
For E.G.
{
"id": 1,
"name": "Foo",
"price": 123,
"tags": [ "Bar", "Eek" ],
"stock": {
"warehouse": 300,
"retail": 20
}
}
No use of eval or any built in function or library like json. Can I use regex here?
How do I do this?
You have JSON data, use the json library to parse it:
import json
data = json.loads(yourstring)
Python comes with batteries included, don't reinvent the wheel.

Categories

Resources