So I tried store 'number' in JSON, but I still don't know how I can update it. It should look something like on command it read number from JSON (some code) and on the end it's adding 1 to this number and when you use again this command it will show number increased by 1.
Can somebody help me with this?
You can keep your JSON data as a Python dictionary. If your data is stored as a JSON, then you can convert it to a dictionary with the json library.
from json import loads
# Data as JSON string
json_data = '{"number": 0}'
# Convert to a dictionary
python_dictionary = loads(json_data)
Afterwards, you can alter the JSON (now dictionary) values by calling them changing them in the dictionary.
# Increment
python_dictionary["number"] += 1
Finally, when you need to use the data in JSON format again, you can convert it back like so:
# Convert back to json
from json import dumps
new_json = dumps(python_dictionary)
Related
I am trying to get all of the data stored in this json
as a dictionary that I can load and access. I am still new to writing spiders, but I believe I need something like
response.xpath().extract()
and then json.load().split() to get an element from it.
But the exact syntax I am not sure of, since there are so many elements in this file.
You can use re_first() to extract JSON from JavaScript code and next loads() it using json module:
import json
d = response.xpath('//script[contains(., "windows.PAGE_MODEL")]/text()').re_first(r'(?s)windows.PAGE_MODEL = (.+?\});')
data = json.loads(d)
property_id = data['propertyData']['id']
You're right, it pretty much works like you suggested in your question.
You can check the script tags for 'windows.PAGE_MODEL' with a simple xpath query.
Please try the following code in the callback for your request:
d = response.xpath('//script[text()[contains(., "windows.PAGE_MODEL")]]/text()').get()
from json import loads
data = loads(d)
Can't solve how to convert JSON to python so that all data be in an array.
I used code to extract JSON, but the problem is that to extract strings from each new JSON data set is a new issue due to the inequality of the number of columns.
import json
data = open('acndata_sessions.json')
json.load(data)
I also tried to use https://app.quicktype.io/, but the function result is:
data_from_dict(json.loads(json_string)) doesn't work.
Data set: json
This question seems to have been asked before. Convert JSON array to Python list
They use 'json.loads' instead of 'json.load' which you use. Both are functions, but they are different.
I think your looking for something like this.
import json
with open('myfile.json','r') as jsonFile:
pythonJSON=json.load(jsonFile)
jsonFile.close()
print(pythonJSON.keys())
The json.loads() is used when you have a string type. If the example above doesn't work with just json.load() try it with json.loads().
json.load already gives you a dictionary. You just have to use it and iterate through _items
import json
data = open('acndata_sessions.json')
data_dict = json.load(data)
# Load items from dictionary
items = data_dict['_items']
# Iterate in items
for item in items:
# Print the item
print(item)
# Or if you want to further iterate on user inputs present in this item
user_inputs = item['userInputs']
# Check if there are any user inputs present in this item
if user_inputs:
for user_input in user_inputs:
print(user_input)
Try checking this question. You can parse a json file like this:
import json
# read file
with open('example.json', 'r') as myfile:
data=myfile.read()
# parse file
obj = json.loads(data)
# show values
print(str(obj['_items']))#returns a dict
print(str(obj['_meta']))
I collected some tweets from the twitter API and stored it to mongodb, I tried exporting the data to a JSON file and didn't have any issues there, until I tried to make a python script to read the JSON and convert it to a csv. I get this traceback error with my code:
json.decoder.JSONDecodeError: Extra data: line 367 column 1 (char 9745)
So, after digging around the internet I was pointed to check the actual JSON data in an online validator, which I did. This gave me the error of:
Multiple JSON root elements
from the site https://jsonformatter.curiousconcept.com/
Here are pictures of the 1st/2nd object beginning/end of the file:
or a link to the data here
Now, the problem is, I haven't found anything on the internet of how to handle that error. I'm not sure if it's an error with the data I've collected, exported, or if I just don't know how to work with it.
My end game with these tweets is to make a network graph. I was looking at either Networkx or Gephi, which is why I'd like to get a csv file.
Robert Moskal is right. If you can address the issue at source and use --jsonArray flag when you use mongoexport then it will make the problem easier i guess. If you can't address it at source then read the below points.
The code below will extract you the individual json objects from the given file and convert them to python dictionaries.
You can then apply your CSV logic to each individual dictionary.
If you are using csv module then I would say use unicodecsv module as it would handle the unicode data in your json objects.
import json
with open('path_to_your_json_file', 'rb') as infile:
json_block = []
for line in infile:
json_block.append(line)
if line.startswith('}'):
json_dict = json.loads(''.join(json_block))
json_block = []
print json_dict
If you want to convert it to CSV using pandas you can use the below code:
import json, pandas as pd
with open('path_to_your_json_file', 'rb') as infile:
json_block = []
dictlist=[]
for line in infile:
json_block.append(line)
if line.startswith('}'):
json_dict = json.loads(''.join(json_block))
dictlist.append(json_dict)
json_block = []
df = pd.DataFrame(jsonlist)
df.to_csv('out.csv',encoding='utf-8')
If you want to flatten out the json object you can use pandas.io.json.json_normalize() method.
Elaborating on #MYGz suggestion to use --jsonArray
Your post doesn't show how you exported the data from mongo. If you use the following via the terminal, you will get valid json from mongodb:
mongoexport --collection=somecollection --db=somedb --jsonArray --out=validfile.json
Replace somecollection, somedb and validfile.json with your target collection, target database, and desired output filename respectively.
The following: mongoexport --collection=somecollection --db=somedb --out=validfile.json...will NOT give you the results you are looking for because:
By default mongoexport writes data using one JSON document for every
MongoDB document. Ref
A bit late reply, and I am not sure it was available the time this question was posted. Anyway, now there is a simple way to import the mongoexport json data as follows:
df = pd.read_json(filename, lines=True)
mongoexport provides each line as a json objects itself, instead of the whole file as json.
The game is about tamaguchis, and I want the tamaguchi to remember its last size and it's 3 last actions the next time it plays. I also want the date to matter, like if you don't play with it for a week it shrinks in size. So first step I thought was to save down all the relevant data to a text file, and each time the game starts the code searchs through the text file and extracts the relevant data again! But I can't even get step 1 working :( I mean, I don't get why this doesn't work??
file = open("Tamaguchis.txt","w")
date = time.strftime("%c")
dictionary = {"size":tamaguchin.size,"date":date,"order":lista}
file.write(dictionary)
it says that it can't export dictonaries, only strings to a text file. But that's not correct is it, I thought you were supposed to be able to put dictionaries in text files? :o
If anyone also has an idea on how calculate the difference between the current date, and the date saved in the text file, that'd be much appriciated :)
Sorry if noob question, and thanks a lot!
If your dictionary consists only of simple python objects, you can use json module to serialize it and write into a file.
import json
with open("Tamaguchis.txt","w") as file:
date = time.strftime("%c")
dictionary = {"size":tamaguchin.size,"date":date,"order":lista}
file.write(json.dumps(dictionary))
The same can be then read with loads.
import json
with open("Tamaguchis.txt","r") as file:
dictionary = json.loads(file.read())
If your dictionary may contain more complex objects, you can either define json serializer for them, or use pickle module. Note, that the latter might make it possible to invoke arbitrary code if not used properly.
You need to convert the dict to a string:
file.write(str(dictionary))
... though you might want to use pickle, json or yaml for the task - reading back is easier/safer then.
Oh,, for date and time caculations you might want to check the timedelta module.
import pickle
a = {'a':1, 'b':2}
with open('temp.txt', 'w') as writer:
data = pickle.dumps(a)
writer.write(data)
with open('temp.txt', 'r') as reader:
data2= pickle.loads(reader.read())
print data2
print type(data2)
Output:
{'a': 1, 'b': 2}
<type 'dict'>
If you care efficiency, ujson or cPinkle is better.
I'm trying to write a simple JSON to CSV converter in Python for Kiva. The JSON file I am working with looks like this:
{"header":{"total":412045,"page":1,"date":"2012-04-11T06:16:43Z","page_size":500},"loans":[{"id":84,"name":"Justine","description":{"languages":["en"], REST OF DATA
The problem is, when I use json.load, I only get the strings "header" and "loans" in data, but not the actual information such as id, name, description, etc. How can I skip over everything until the [? I have a lot of files to process, so I can't manually delete the beginning in each one. My current code is:
import csv
import json
fp = csv.writer(open("test.csv","wb+"))
f = open("loans/1.json")
data = json.load(f)
f.close()
for item in data:
fp.writerow([item["name"]] + [item["posted_date"]] + OTHER STUFF)
Instead of
for item in data:
use
for item in data['loans']:
The header is stored in data['header'] and data itself is a dictionary, so you'll have to key into it in order to access the data.
data is a dictionary, so for item in data iterates the keys.
You probably want for loan in data['loans']: