How to add an element to an empty JSON in python? - python

I created an empty string & convert it into a JSON by json.dump. Once I want to add element, it fails & show
AttributeError: 'str' object has no attribute 'append'
I tried both json.insert & json.append but neither of them works.
It seems that it's data type problem. As Python can't declare data type as Java & C can, how can I avoid the problem?
import json
data = {}
json_data = json.dumps(data)
json_data.append(["A"]["1"])
print (json_data)

JSON is a string representation of data, such as lists and dictionaries. You don't append to the JSON, you append to the original data and then dump it.
Also, you don't use append() with dictionaries, it's used with lists.
data = {} # This is a dictionary
data["a"] = "1"; # Add an item to the dictionary
json_data = json.dumps(data) # Convert the dictionary to a JSON string
print(json_data)

Related

I want to convert a list of strings to a list of dictionaries

I stored a twitter data to a mysql db as a json. When I fetch it back, it returns a list of string instead of a list of dictionary. I am looking for a way turn it to a list of dictionaries. This is the format i get the stored data back. "tweetdata" is the column name in the db
[{"tweetdata":"[{\"text\":\"b problem\",\"len\":10,\"Date\":1583160242000,\"Source\":\"Twitter for
Android\",\"Likes\":0,\"RTs\":0},}]"}]
I want it to return something like this as a list of dicts with the column name stripped off
[{\"text\":\"b problem\",\"len\":10,\"Date\":1583160242000,\"Source\":\"Twitter for
Android\",\"Likes\":0,\"RTs\":0},}]
First of all, looks like you provided a wrong json format. Provided that you have a correct json format then you can use json loads function to load the the json data and convert it to dictionary type. Here is code snippet in python.
import json
json_data = '[{"tweetdata":[{\"text\":\"b problem\",\"len\":10,\"Date\":1583160242000,\"Source\":\"Twitter for Android\",\"Likes\":0,\"RTs\":0}]}]'
parsed_json = json.loads(json_data)
parsed_dict = parsed_json[0]['tweetdata'][0]
print(type(parsed_dict))
for item in parsed_dict.items():
print(item)
Above code snippet will print these.
<class 'dict'>
('text', 'b problem')
('len', 10)
('Date', 1583160242000)
('Source', 'Twitter for Android')
('Likes', 0)
('RTs', 0)
Try this:
if your data in tweetdata variable then tweetdata[0]["tweetdata"]
it'll return like this:
[{\"text\":\"b problem\",\"len\":10,\"Date\":1583160242000,\"Source\":\"Twitter for Android\",\"Likes\":0,\"RTs\":0},}]
actually you can do like this:
data = [{"tweetdata":"[{\"text\":\"b problem\",\"len\":10,\"Date\":1583160242000,\"Source\":\"Twitter for Android\",\"Likes\":0,\"RTs\":0},}]"}][0]["tweetdata"]
and print data you'll get the same result.

Reading JSON / array of JSONs from set of files

I have a series of files, each with a JSON, that I'd like to read into Pandas. This is pretty straightforward:
data_unfiltered = [json.load(open(jf)) for jf in json_input_files]
# next call used to be df = pandas.DataFrame(data_unfiltered)
# instead, json_normalize flattens nested dicts
df = json_normalize(data_unfiltered)
Now, a new wrinkle. Some of these input files no longer have just a plain JSON but instead a (python) list of JSONs: [ { JSON }, { JSON }... ].
json.load is pretty great because it inputs a whole file and puts it straight into JSON; I don't have to parse the file at all. How would I now turn a list of files, some of which have a single JSON object and some of which have a list of JSON objects, into a list of parsed JSON objects?
Bonus question: I used to be able to add the filename into each JSON with
df['filename'] = pandas.Series(json_input_files).values
but now I can't do that any more since now one input file might correspond to many JSONs in the output list. How can I add the filenames to the JSONs before I put them into a pandas dataframe?
Edit: Work in progress, per request in comments:
data_unfiltered = []
for jf_file in json_input_files:
jf = open(jf_file)
if isinstance(jf, list): # this is obviously wrong
for j in jf:
d = json.load(j) # this is what I need to fix
d['details'] = jf_file
data_unfiltered.append(d)
else: # not a list, assume dict
d = json.load(jf)
d['details'] = jf_file
data_unfiltered.append(d)
but json.load() worked perfectly for what I wanted (file object to JSON) and has no equiv for arrays. I figure I have to manually parse the file into a list of blobs and then do json.loads() on each blob? That's pretty kludgey though.

Python 3. Extract data from json

How to extract 41677?
My json:
{"41677":{"key":"ilya","premium":"true"}}
My code:
params={"id": "ilya", "fmt": "json"}
r=requests.get("somesite", params=params )
data=json.loads(r.text)
By using loads, your JSON string will be converted to a dictionary which maps keys to values.
Since you need the key 41677, you can simply call data.keys()[0] to retrieve the first key of your dictionary.
EDIT:
Also, if you have a list of that JSON structure, you can iterate over the keys and values using the items function, like so:
for key, value in data.items():
print key # 41677
print value # {"key":"ilya","premium":"true"}
By using Requests' built-in json attribute:
data = requests.get("somesite", params=params ).json().keys()[0]
assuming the json it returns is {"41677":{"key":"ilya","premium":"true"}}:
>>>print data
"41677"
import json
s = {"41677":{"key":"ilya","premium":"true"}}
d = json.dumps(s)
l = json.loads(d)
l.keys()

Error: Set not JSON Serializable while converting TSV File into JSON Format using Python

I'm looking to convert a TSV File I have into JSON Format for mapping (google fusion maps doesn't support mapping multiple objects in the same location so I'm converting it to JSON format to try on Mapbox). Here's my TSV file if you're curious:
https://github.com/yongcho822/Movies-in-the-park/blob/master/MovieParksGeocodeTest.tsv
And here is my corresponding python code thus far:
import json
import csv
def create_map(datafile):
geo_map = {"type":"FeatureCollection"}
item_list = []
with open(datafile, 'r') as tsvfile:
reader = csv.DictReader(tsvfile, delimiter = '\t')
for i, line in enumerate(reader):
data = {}
data['type'] = 'Feature'
data['id'] = i
data['properties']={'title': line['Movie Title'],
'description': line['Amenities'],
'date': line['Date']}
data['name'] = {line['Location']}
data['geometry'] = {'type':'Point',
'coordinates':(line['Lat'], line['Lng'])}
item_list.append(data)
#print item_list
for point in item_list:
geo_map.setdefault('features', []).append(point)
print 'CHECKPOINT'
with open("thedamngeojson.geojson", 'w') as f:
f.write(json.dumps(geo_map))
create_map('MovieParksGeocodeTest.tsv')
It's throwing me an error at the end (after it prints CHECKPOINT), saying
TypeError: set(['Edgebrook Park, Chicago ']) is not JSON serializable
I figure the last two lines is where the error is.. but what's wrong and how do I fix it??
JSON is designed to be a very simple, very portable format; the only kinds of values it understands are strings, numbers, booleans, null (like Python None), objects (like a Python dict) and arrays (like a Python list).
But at least one of the values in your dictionary is a set:
data['name'] = {line['Location']}
Since JSON doesn't have a set type, you get an error telling you that set … is not JSON serializable.
If you don't actually need this to be a set instead of a list (which you probably don't—if it really only ever have one element, who cares which collection type it is?), the easy answer is to change it to be a list:
data['name'] = [line['Location']]
(In fact, even when you do need this to be a set during processing, you usually don't need it to be a set during storage/interchange. If the consumer of your file needs to use it as a set, it can always convert it back later.)

How can I create the empty json object?

I have this code
json.loads(request.POST.get('mydata',dict()))
But I get this error
No JSON object could be decoded
I just want that if don't have mydata in POST, then I don't get that error.
Simply:
json.loads(request.POST.get('mydata', '{}'))
Or:
data = json.loads(request.POST['mydata']) if 'mydata' in request.POST else {}
Or:
if 'mydata' in request.POST:
data = json.loads(request.POST['mydata'])
else:
data = {} # or data = None
loads() takes a json formatted string and turns it into a Python object like dict or list. In your code, you're passing dict() as default value if mydata doesn't exist in request.POST, while it should be a string, like "{}". So you can write -
json_data = json.loads(request.POST.get('mydata', "{}"))
Also remember, the value of request.POST['mydata'] must be JSON formatted, or else you'll get the same error.

Categories

Resources