Regarding Json load and dump - python

I am trying to substitute a value using safe substitute. Before this, I am converting an array using JSON dumps and then substituting. Once the substitution is done I am doing JSON loads and passing as a parameter to other utility. While doing this I am getting an error for JSON loads. Below is the code...
account_id={'ABC123', user_id='testing'}
var1 = {'account':account_id, 'user':user_id}
response = json.dumps(var1)
payload = Template.(test_template).safe_substitute(var1=var1)
output = json.loads(payload)
get an error when it comes to loads:
Expecting "," delimiter: line 1 column 448 (char 447)

It's seems to be a syntax error. Try, like below
account_id='ABC123'
user_id='testing'
var1 = {'account':account_id, 'user':user_id}
response = json.dumps(var1)
print(response)
# out: '{"account": "ABC123", "user": "testing"}'
output = json.loads(response)
print(output)
# out: {'user': 'testing', 'account': 'ABC123'}

Related

Why am I getting this error "TypeError: string indices must be integers" when trying to fetch data from an api?

json file =
{
"success": true,
"terms": "https://curr
"privacy": "https://cu
"timestamp": 162764598
"source": "USD",
"quotes": {
"USDIMP": 0.722761,
"USDINR": 74.398905,
"USDIQD": 1458.90221
}
}
The json file is above. i deleted lot of values from the json as it took too many spaces. My python code is in below.
import urllib.request, urllib.parse, urllib.error
import json
response = "http://api.currencylayer.com/live?access_key="
api_key = "42141e*********************"
parms = dict()
parms['key'] = api_key
url = response + urllib.parse.urlencode(parms)
mh = urllib.request.urlopen(url)
source = mh.read().decode()
data = json.loads(source)
pydata = json.dumps(data, indent=2)
print("which curreny do you want to convert USD to?")
xm = input('>')
print(f"Hoe many USD do you want to convert{xm}to")
value = input('>')
fetch = pydata["quotes"][0]["USD{xm}"]
answer = fetch*value
print(fetch)
--------------------------------
Here is the
output
"fetch = pydata["quotes"][0]["USD{xm}"]
TypeError: string indices must be integers"
First of all the JSON data you posted here is not valid. There are missing quotes and commas. For example here "terms": "https://curr. It has to be "terms": "https://curr",. The same at "privacy" and the "timestamp" is missing a comma. After i fixed the JSON data I found a solution. You have to use data not pydata. This mean you have to change fetch = pydata["quotes"][0]["USD{xm}"] to fetch = data["quotes"][0]["USD{xm}"]. But this would result in the next error, which would be a KeyError, because in the JSON data you provided us there is no array after the "qoutes" key. So you have to get rid of this [0] or the json data has to like this:
"quotes":[{
"USDIMP": 0.722761,
"USDINR": 74.398905,
"USDIQD": 1458.90221
}]
At the end you only have to change data["quotes"]["USD{xm}"] to data["quotes"]["USD"+xm] because python tries to find a key called USD{xm} and not for example USDIMP, when you type "IMP" in the input.I hope this fixed your problem.

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 2 column 2 (char 3)

I know this Question is already answered, but I dont know where the Error is in my Case.
This is my Code:
import json
json_data = """
{
'position1': '516, 440',
'position2': '971, 443',
'position3': '1186, 439',
'position4': '1402, 441',
'position5': '1630, 449',
'position6': '299, 681',
'position7': '518, 684',
'position8': '736, 691',
'position9': '739, 431'
}
"""
data = json.loads(json_data)
print(data)
Im not really into working with json files, so please don't blame me if it's a really dump mistake.
Don't use triple quotes """. Instead use a dictionary with json.dumps() so that commas in your values are not misinterpreted as commas between items.
import json
json_data = {
'position1': '516, 440',
'position2': '971, 443',
'position3': '1186, 439',
'position4': '1402, 441',
'position5': '1630, 449',
'position6': '299, 681',
'position7': '518, 684',
'position8': '736, 691',
'position9': '739, 431'
}
data = json.dumps(json_data)
print(data)
If you are using triple quotes, this would work
json_data = json_data.replace("'", '"')
data = json.loads(json_data)
print(data)
Try this one
import json
json_data = {
'position1': '516, 440',
'position2': '971, 443',
'position3': '1186, 439',
'position4': '1402, 441',
'position5': '1630, 449',
'position6': '299, 681',
'position7': '518, 684',
'position8': '736, 691',
'position9': '739, 431'
}
data = json.dumps(json_data)
print(data)

Convert website JSON data to CSV python, returns JSON decode error

I am trying to convert the JSON at this URL: https://wtrl.racing/assets/js/miscttt170620.php?wtrlid=63, which I have saved in a file, to a CSV using this code:
json_data_file = open('TTT json', 'r')
content = json.load(json_data_file)
csv_results = csv.writer(open("TTT_results.csv.csv", "w", newline=''))
for item in content:
print(item)
csv_results.writerow(item)
This returns: json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 489351 (char 489350), which is the '2' in this section of JSON "ll": 43.529}, {"aa":
I'm bemused as to why this would be.
Looks like the json is malformed. You need to contact the person that generated this json to fix it.
See the entries:
"cc": "Nick "Lionel" Berry(TriTalk)"
"cc": ""Sherpa" Dave (R&K Hyenas)"
These quotes are not properly escaped. It needs to be:
"cc": "Nick \"Lionel\" Berry(TriTalk)"
"cc": "\"Sherpa\" Dave (R&K Hyenas)"
Looks like you're probably requesting the wrong accept encoding when accessing the data. Try downloading it by specifying that you want a JSON response and not an HTML payload:
import requests
url = 'https://wtrl.racing/assets/js/miscttt170620.php?wtrlid=63'
headers = {'Accept': 'application/json'}
response = requests.get(url, headers=headers)
data = response.json()

Read from JSON file with multiple objects in Python

I have a problem regarding JSON library in Python. I can't figure out a way to read data from json file that looks like this:
{"name": "LOTR", "author": "Tolkin"}{"name": "Aska", "author": "Ivo"}
because when I try to load data using this code:
with open("json_books.txt","r") as file:
json_data = json.load(file)
I get the following error:
json.decoder.JSONDecodeError: Extra data: line 1 column 37 (char 36)
I've looked it up and none of the solutions I found helped me. If anyone can help me with this one it would be much appreciated.
You can read the file content as a string, extract the "char" number, which is an index, from the error message of the JSONDecodeError exception, and reparse the slice of the string up to that index as valid JSON, and parse the rest of the string in the same way, until it no longer raises an error:
import json
import re
s = '{"name": "LOTR", "author": "Tolkin"}{"name": "Aska", "author": "Ivo"}'
json_data = []
while True:
try:
json_data.append(json.loads(s))
break
except json.JSONDecodeError as e:
match = re.match(r'Extra data: .*\(char (\d+)\)', str(e))
if match:
index = int(match.group(1))
json_data.append(json.loads(s[:index]))
s = s[index:]
else:
raise
print(json_data)
This outputs:
[{'name': 'LOTR', 'author': 'Tolkin'}, {'name': 'Aska', 'author': 'Ivo'}]
What you listed is not valid JSON. JSON must have a single list or object at top level -- this has two objects.
Perhaps the proper JSON would instead be:
[{"name": "LOTR", "author": "Tolkin"}, {"name": "Aska", "author": "Ivo"}]

Python JSON decoder error with unicode characters in request content

Using requests library to execute http GET that return JSON response i'm getting this error when response string contains unicode char:
json.decoder.JSONDecodeError: Invalid control character at: line 1 column 20 (char 19)
Execute same http request with Postman the json output is:
{ "value": "VILLE D\u0019ANAUNIA" }
My python code is:
data = requests.get(uri, headers=HEADERS).text
json_data = json.loads(data)
Can I remove or replace all Unicode chars before executing conversion with json.loads(...)?
It is likely to be caused by a RIGHT SINGLE QUOTATION MARK U+2019 (’). For reasons I cannot guess, the high order byte has been dropped leaving you with a control character which should be escaped in a correct JSON string.
So the correct way would be to control what exactly the API returns. If id does return a '\u0019' control character, you should contact the API owner because the problem should be there.
As a workaround, you can try to limit the problem for your processing by filtering out non ascii or control characters:
data = requests.get(uri, headers=HEADERS).text
data = ''.join((i for i in data if 0x20 <= ord(i) < 127)) # filter out unwanted chars
json_data = json.loads(data)
You should get {'value': 'VILLE DANAUNIA'}
Alternatively, you can replace all unwanted characters with spaces:
data = requests.get(uri, headers=HEADERS).text
data = ''.join((i if 0x20 <= ord(i) < 127 else ' ' for i in data))
json_data = json.loads(data)
You would get {'value': 'VILLE D ANAUNIA'}
The code below works on python 2.7:
import json
d = json.loads('{ "value": "VILLE D\u0019ANAUNIA" }')
print(d)
The code below works on python 3.7:
import json
d = json.loads('{ "value": "VILLE D\u0019ANAUNIA" }', strict=False)
print(d)
Output:
{u'value': u'VILLE D\x19ANAUNIA'}
Another point is that requests get return the data as json:
r = requests.get('https://api.github.com/events')
r.json()

Categories

Resources