Converting JSON String to Dictionary Not List
Your JSON is an array with a single object inside, so when you read it in you get a list with a dictionary inside. You can access your dictionary by accessing item 0 in the list, as shown below:
json1_data = json.loads(json1_str)[0]
Now you can access the data stored in datapoints just as you were expecting:
datapoints = json1_data['datapoints']
I have one more question if anyone can bite: I am trying to take the average of the first elements in these datapoints(i.e. datapoints[0][0]). Just to list them, I tried doing datapoints[0:5][0] but all I get is the first datapoint with both elements as opposed to wanting to get the first 5 datapoints containing only the first element. Is there a way to do this?
datapoints[0:5][0]
doesn't do what you're expecting. datapoints[0:5]
returns a new list slice containing just the first 5 elements, and then adding [0]
on the end of it will take just the first element from that resulting list slice. What you need to use to get the result you want is a list comprehension:
[p[0] for p in datapoints[0:5]]
Here's a simple way to calculate the mean:
sum(p[0] for p in datapoints[0:5])/5. # Result is 35.8
If you're willing to install NumPy, then it's even easier:
import numpy
json1_file = open('json1')
json1_str = json1_file.read()
json1_data = json.loads(json1_str)[0]
datapoints = numpy.array(json1_data['datapoints'])
avg = datapoints[0:5,0].mean()
# avg is now 35.8
Using the ,
operator with the slicing syntax for NumPy's arrays has the behavior you were originally expecting with the list slices.
How convert a JSON string to Dictionary in Python?
Surround your original string with square brackets to make it a valid JSON string:
import json
valid_json_string = "[" + your_string + "]" # or "[{0}]".format(your_string)
data = json.loads(valid_json_string)
Convert JSON string to dict using Python
json.loads()
import json
d = json.loads(j)
print d['glossary']['title']
Python convert string holding nested json to dict
Your nested key seems like a JSON string that can be loaded into a dictionary using json.loads
method.
Though the nested JSON won't get converted to the dictionary that's why I've added the recursive function to address the nested dictionary present in the JSON.
import json
from json import JSONDecodeError
def recurse(d):
try:
if isinstance(d, dict):
loaded_d = d
else:
loaded_d = json.loads(d)
for k, v in loaded_d.items():
loaded_d[k] = recurse(v)
except (JSONDecodeError, TypeError):
return d
return loaded_d
for d in data_list:
for key, val in d.items():
d[key] = recurse(val)
Output:
[
{
"keyA": "Example",
"keyB": {"keyC": 2, "keyD": {"keyE": {"name": "foo"}}, "keyF": 0},
},
{
"keyA": "Example2",
"keyB": {"keyC": 6, "keyD": {"keyE": {"name": "bar"}}, "keyF": 5},
},
]
Convert json strings to a single dictionary in python
Try this,
import json
d = {}
with open('file1.txt') as f:
for line in f:
data = json.loads(line)
for k, v in data.items():
d.setdefault(k,[]).append(v)
or you can use defaultdict to define empty dict with values as list.
import json
from collections import defaultdict
d = defaultdict(list)
with open('file1.txt') as f:
for line in f:
data = json.loads(line)
for k, v in data.items():
d[k].append(v)
Output:
print(d)
{'_id': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'name': ['aimee Zank', 'Aurelia Menendez', 'Corliss Zuk', 'Bao Ziglar', 'Zachary Langlais', 'Wilburn Spiess', 'Jenette Flanders', 'Salena Olmos', 'Daphne Zheng', 'Sanda Ryba'], 'scores': [[{'score': 1.463179736705023, 'type': 'exam'}, {'score': 11.78273309957772, 'type': 'quiz'}, {'score': 35.8740349954354, 'type': 'homework'}], [{'score': 60.06045071030959, 'type': 'exam'}, {'score': 52.79790691903873, 'type': 'quiz'}, {'score': 71.76133439165544, 'type': 'homework'}], [{'score': 67.03077096065002, 'type': 'exam'}, {'score': 6.301851677835235, 'type': 'quiz'}, {'score': 66.28344683278382, 'type': 'homework'}], [{'score': 71.64343899778332, 'type': 'exam'}, {'score': 24.80221293650313, 'type': 'quiz'}, {'score': 42.26147058804812, 'type': 'homework'}], [{'score': 78.68385091304332, 'type': 'exam'}, {'score': 90.2963101368042, 'type': 'quiz'}, {'score': 34.41620148042529, 'type': 'homework'}], [{'score': 44.87186330181261, 'type': 'exam'}, {'score': 25.72395114668016, 'type': 'quiz'}, {'score': 63.42288310628662, 'type': 'homework'}], [{'score': 37.32285459166097, 'type': 'exam'}, {'score': 28.32634976913737, 'type': 'quiz'}, {'score': 81.57115318686338, 'type': 'homework'}], [{'score': 90.37826509157176, 'type': 'exam'}, {'score': 42.48780666956811, 'type': 'quiz'}, {'score': 96.52986171633331, 'type': 'homework'}], [{'score': 22.13583712862635, 'type': 'exam'}, {'score': 14.63969941335069, 'type': 'quiz'}, {'score': 75.94123677556644, 'type': 'homework'}], [{'score': 97.00509953654694, 'type': 'exam'}, {'score': 97.80449632538915, 'type': 'quiz'}, {'score': 25.27368532432955, 'type': 'homework'}]]}
Useful way to convert string to dictionary using python
Your approach is good, except for a couple weird things:
- You aren't creating a JSON anything, so to avoid any confusion I suggest you don't name your returned dictionary
json_data
or your functionstr_2_json
. JSON, or JavaScript Object Notation is just that -- a standard of denoting an object as text. The objects themselves have nothing to do with JSON. - You can use
i.strip()
instead of joining the splitted string (not sure why you did it this way, since you commented outi.strip()
) - Some of your values contain multiple spaces (e.g.
"size 4764771 MB"
or"disks /dev/sde /dev/sdf /dev/sdg"
). By your code, you end up everything after the second space in such strings. To avoid this, dostripped_str.split(' ', 1)
which limits how many times you want to split the string.
Other than that, you could create a dictionary in one line using the dict()
constructor and a generator expression:
def str_2_dict(string):
data = dict(item.strip().split(' ', 1) for item in string.split(','))
return data
print(str_2_dict('name SP2, status Online, size 4764771 MB, free 2576353 MB, path /dev/sde, log 210 MB, port 5660, guid 7478a0141b7b9b0d005b30b0e60f3c4d, clusterUuid -8650609094877646407--116798096584060989, disks /dev/sde /dev/sdf /dev/sdg, dare 0'))
Outputs:
{
'name': 'SP2',
'status': 'Online',
'size': '4764771 MB',
'free': '2576353 MB',
'path': '/dev/sde',
'log': '210 MB',
'port': '5660',
'guid': '7478a0141b7b9b0d005b30b0e60f3c4d',
'clusterUuid': '-8650609094877646407--116798096584060989',
'disks': '/dev/sde /dev/sdf /dev/sdg',
'dare': '0'
}
This is probably the same (practically, in terms of efficiency / time) as writing out the full loop:
def str_2_dict(string):
data = dict()
for item in string.split(','):
key, value = item.strip().split(' ', 1)
data[key] = value
return data
String to Dictionary in Python
This data is JSON! You can deserialize it using the built-in json
module if you're on Python 2.6+, otherwise you can use the excellent third-party simplejson
module.
import json # or `import simplejson as json` if on Python < 2.6
json_string = u'{ "id":"123456789", ... }'
obj = json.loads(json_string) # obj now contains a dict of the data
Related Topics
Why Does Random.Shuffle Return None
Difference Between _Getattr_ and _Getattribute_
What's the Purpose of "Send" Function on Python Generators
Pandas: Filter Rows of Dataframe with Operator Chaining
How to Install Packages Using Pip According to the Requirements.Txt File from a Local Directory
How to Get Attribute of Element from Selenium
Sqlite Parameter Substitution Problem
Numpy Where Function Multiple Conditions
What's the Scope of a Variable Initialized in an If Statement
Multiprocessing Global Variable Updates Not Returned to Parent
Elegant Ways to Support Equivalence ("Equality") in Python Classes
How to Use Stringio in Python3
How to Convert SQLalchemy Row Object to a Python Dict
How to Check If the String Is Empty
Confused About Backslashes in Regular Expressions