json_data = [] # your list with json objects (dicts)
with open('prueba.json') as json_file:
json_data = json.load(json_file)
for item in json_data:
for data_item in item['data']:
print data_item['name'], data_item['value']
Answer from konart on Stack Overflowjson_data = [] # your list with json objects (dicts)
with open('prueba.json') as json_file:
json_data = json.load(json_file)
for item in json_data:
for data_item in item['data']:
print data_item['name'], data_item['value']
Something like
for key, value in json_data.iteritems():
print key
if isinstance(value, (list, tuple)):
for item in value:
print item
if isinstance(value, (dict)):
for value_key,value_value in value.iteritems():
print value_key,str(value_value)
Can be improved to manage more types and can be made recursive.
How do I read a list of JSON files from file in python? - Stack Overflow
Convert JSON array to Python list - Stack Overflow
How to parse JSON file into Python list - Stack Overflow
List all JSON keys in a file to identify database column names from a file using Python
Videos
You're trying to read the string "data.txt". What you want is to open and read the file.
import json
with open('data.txt', 'r') as data_file:
json_data = data_file.read()
data = json.loads(json_data)
Try:
data = json.load(open("data.txt", 'r'))
json.loads interprets a string as JSON data, while json.load takes a file object and reads it, then interprets it as JSON.
import json
array = '{"fruits": ["apple", "banana", "orange"]}'
data = json.loads(array)
print(data['fruits'])
# the print displays:
# ['apple', 'banana', 'orange']
You had everything you needed. data will be a dict, and data['fruits'] will be a list
Tested on Ideone.
import json
array = '{"fruits": ["apple", "banana", "orange"]}'
data = json.loads(array)
fruits_list = data['fruits']
print fruits_list
There's just a slight problem with your for loop.
james@VIII:~/Desktop$ ls
f.txt
james@VIII:~/Desktop$ cat f.txt
{
"Ask":
{"0":[[9.13,30200],[9.14,106946],[9.15,53072],[9.16,58104],[9.17,45589]],
"1":[[9.14,106946],[9.15,53072],[9.16,58104],[9.17,45589],[9.18,37521]] },
"Bid":
{"0":[[9.12,198807],[9.11,1110],[9.1,42110],[9.09,84381],[9.08,98178]],
"1":[[9.13,13500],[9.12,198807],[9.11,1110],[9.1,42110],[9.09,84381]]}
}
james@VIII:~/Desktop$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> with open('f.txt') as f_in:
... data = json.load(f_in)
...
>>> data
{'Ask': {'0': [[9.13, 30200], [9.14, 106946], [9.15, 53072], [9.16, 58104], [9.17, 45589]], '1': [[9.14, 106946], [9.15, 53072], [9.16, 58104], [9.17, 45589], [9.18, 37521]]}, 'Bid': {'0': [[9.12, 198807], [9.11, 1110], [9.1, 42110], [9.09, 84381], [9.08, 98178]], '1': [[9.13, 13500], [9.12, 198807], [9.11, 1110], [9.1, 42110], [9.09, 84381]]}}
>>> data['Ask']
{'0': [[9.13, 30200], [9.14, 106946], [9.15, 53072], [9.16, 58104], [9.17, 45589]], '1': [[9.14, 106946], [9.15, 53072], [9.16, 58104], [9.17, 45589], [9.18, 37521]]}
>>>
>>> data['Bid']
{'0': [[9.12, 198807], [9.11, 1110], [9.1, 42110], [9.09, 84381], [9.08, 98178]], '1': [[9.13, 13500], [9.12, 198807], [9.11, 1110], [9.1, 42110], [9.09, 84381]]}
>>> for x in data['Bid']['0']:
... print(x)
...
[9.12, 198807]
[9.11, 1110]
[9.1, 42110]
[9.09, 84381]
[9.08, 98178]
Your for loop just needed to be changed a little.
PS you don't need to specify 'r' when reading the file.
You can also get individual values like this:
>>> for x in data['Bid']['0']:
... print(str(x[0]) + ': ' + str(x[1]))
...
9.12: 198807
9.11: 1110
9.1: 42110
9.09: 84381
9.08: 98178
your for is loop in the keys of dict.
for x in data["Bid"]:
print(type(x))
# <class 'str'>
try it:
for x in data['Bid']['0']:
print(x)
or
for x in data['Bid'].values():
print(x)
sorry for my English :)
I am learning Python, and in particular, working with JSON and sqlite in Python. Ultimately I plan to use Python to load the JSON into a sqlite database.
Here is the question: Is there a way in to list all of the keys from a JSON file (not from a string) using Python? I want a list of all of the keys so I can determine what columns I will need/use in my sqlite table(s), without having to manually read the file and make a list.
BTW, this is something along the lines of using INFORMATION_SCHEMA.COLUMNS in SQL Server, or the FINDALL in Python for XML.
All of this is for personal learning, so I'm not looking to use other technologies, I'm sticking to Python, JSON, and sqlite on purpose.
I think you have a couple issues going on here. First, valid JSON doesn't use single quotes ('), it is all double quotes ("). You are looking for something like:
[{
"id":123,
"emotions":[],
"lyrics":"AbC",
"emotionID":0,
"artist":"222",
"sentimentScore":0,
"subjects":[],
"synonymKeyWords":[],
"keyWords":[]
},
{
"id":123,
"emotions":[],
"lyrics":"EFG",
"emotionID":0,
"artist":"223",
"sentimentScore":0,
"subjects":[],
"synonymKeyWords":[],
"keyWords":[]
}
]
Secondly, you need to open the json file for reading and then load it as json. The following should work for you:
with open(read_file) as file:
data = json.load(file)
with open(write_file, 'w') as file:
json.dump(data, file)
print(data)
data.append(json.loads(f))
This appends the list you read from the JSON file as a single element to the list. So after your other append, the list will have two elements: One list of songs, and that one song object you added afterwards.
You should use list.extend to extend the list with the items from another list:
data.extends(json.loads(f))
Since your list is empty before that, you can also just load the list from the JSON and then append to that one:
data = json.loads(f)
data.append(vars(songObj))