I'm getting a response from a JSON dumps, and I'm trying to load it in a log.txt file, but this doesn't print out anything
My Function
def get_customer_last_action_executed(self):
"""
Get the customer last action executed
By inputing the CustomerID
:return:
"""
payload ={'customerID': self.CustomerID}
if not self.CustomerID:
customer_id = raw_input("Please provide the customer ID:")
self.CustomerID = customer_id
# raise Exception('No customerID provided')
response = self.send_request(self.get_customer_last_action_executed_url + self.CustomerID,
json.dumps(payload),
"GET")
print response.url, response.status_code
print response, response.text, response.reason
if response:
print self.sucessful_msg
else:
print self.error_msg
with open('log.txt', 'w') as f:
json.dumps(payload, f)
What you need to use is dump() not dumps().
json.dump()
Serialize obj as a JSON formatted stream to fp (a .write()-supporting
file-like object
If ensure_ascii is False, some chunks written to fp may be unicode
instances
json.dumps()
Serialize obj to a JSON formatted str
If ensure_ascii is False, the result may contain non-ASCII characters
and the return value may be a unicode instance
Full details can be found on this thread
Related
I'm getting the following error for my python scraper:
import requests
import json
symbol_id = 'COINBASE_SPOT_BTC_USDT'
time_start = '2022-11-20T17:00:00'
time_end = '2022-11-21T05:00:00'
limit_levels = 100000000
limit = 100000000
url = 'https://rest.coinapi.io/v1/orderbooks/{symbol_id}/history?time_start={time_start}limit={limit}&limit_levels={limit_levels}'
headers = {'X-CoinAPI-Key' : 'XXXXXXXXXXXXXXXXXXXXXXX'}
response = requests.get(url, headers=headers)
print(response)
with open('raw_coinbase_ob_history.json', 'w') as json_file:
json.dump(response.json(), json_file)
with open('raw_coinbase_ob_history.json', 'r') as handle:
parsed = json.load(handle)
with open('coinbase_ob_history.json', 'w') as coinbase_ob:
json.dump(parsed, coinbase_ob, indent = 4)
<Response [400]>
And in my written json file, I'm outputted
{"error": "Wrong format of 'time_start' parameter."}
I assume a string goes into a url, so I flattened the timestring to a string. I don't understand why this doesn't work. This is the documentation for the coinAPI call I'm trying to make with 'timestring'. https://docs.coinapi.io/?python#historical-data-get-4
Incorrect syntax for python. To concatenate strings, stick them together like such:
a = 'a' + 'b' + 'c'
string formatting is invalid, and also need use & in between different url params
# python3
url = f"https://rest.coinapi.io/v1/orderbooks/{symbol_id}/history?time_start={time_start}&limit={limit}&limit_levels={limit_levels}"
# python 2
url = "https://rest.coinapi.io/v1/orderbooks/{symbol_id}/history?time_start={time_start}&limit={limit}&limit_levels={limit_levels}".format(symbol_id=symbol_id, time_start=time_start, limit=limit, limit_levels=limit_levels)
https://docs.python.org/3/tutorial/inputoutput.html
https://docs.python.org/2/tutorial/inputoutput.html
I have a file that has:
{
"name": "HOSTNAME_HTTP",
"description": "Custom hostname for http service route. Leave blank for default hostname, e.g.: \u003capplication-name\u003e-\u003cproject\u003e.\u003cdefault-domain-suffix\u003e"
}
when I open the file using:
with open('data.txt', 'r') as file:
data = file.read()
I pass this to json.loads and the content in data is replaced with:
<application>...</application>
How can I prevent python json.loads from messing with the encoding in the content?
You could use a workaround like this to escape the unicode sequences:
>>> obj = json.loads(data.replace('\\', '\\\\'))
>>> obj
{'name': 'HOSTNAME_HTTP',
'description': 'Custom hostname for http service route. Leave blank for default hostname, e.g.: \\u003capplication-name\\u003e-\\u003cproject\\u003e.\\u003cdefault-domain-suffix\\u003e'}
And then when you're done modifying:
>>> print(json.dumps(obj).replace('\\\\', '\\'))
{"name": "HOSTNAME_HTTP", "description": "Custom hostname for http service route. Leave blank for default hostname, e.g.: \u003capplication-name\u003e-\u003cproject\u003e.\u003cdefault-domain-suffix\u003e"}
If you expect other backslashes in the file, it would be safer to use regular expressions:
import re
from_pattern = re.compile(r'(\\u[0-9a-fA-F]{4})')
to_pattern = re.compile(r'\\(\\u[0-9a-fA-F]{4})')
def from_json_escaped(path):
with open(path, 'r') as f:
return json.loads(from_pattern.sub(r'\\\1', f.read()))
def to_json_escaped(path, obj):
with open(path, 'w') as f:
f.write(to_pattern.sub(r'\1', json.dumps(obj)))
I found a solution:
import json
from json.decoder import JSONDecoder
with open('data.txt', 'r') as file:
data = file.read()
data_without_dump = '{"data":\"' + data + '\"}'
datum_dump = json.dumps(data)
datum = '{"data": ' + datum_dump + '}'
datum_load = json.loads(datum)
datum_load_without_dump = json.loads(data_without_dump)
print(datum_dump)
print(datum)
print(datum_load["data"])
print(datum_load_without_dump["data"])
print(type(datum_dump), type(datum), type(datum_load))
Output:
"\\u003capplication\\u003e.....\\u003c/application\\u003e"
{"data": "\\u003capplication\\u003e.....\\u003c/application\\u003e"}
\u003capplication\u003e.....\u003c/application\u003e
<application>.....</application>
<class 'str'> <class 'str'> <class 'dict'>
My reasoning:
json.loads : Deserialize a str or unicode instance containing a JSON document to a Python object.
json.dumps : Serialize obj to a JSON formatted str.
So, using them in cascading gets the desired result.
I am trying to replace the value of varaible in json object but I am not able to do so.I want to replace the value of a datetime in json_input varaible but as of now datetime is getting concatenated
json_input='{"provider_code":"test","provider_resource_id":{"name":"test","value":"test"},"provider_account":"123","kpis":[{"kpi":"kpis","value":"80","unit":"%","datetime":""}]}'
newjson = {}
try:
decoded = json.loads(json_input)
# Access data
for x in decoded['kpis']:
if not (x['datetime']):
x['datetime']="2019-07-05T18:17:08.257Z"
newjson=json_input+x['datetime']
except (ValueError, KeyError, TypeError):
print("JSON format error")
print(newjson)
*Actual*
new json = {"provider_code":"aws","provider_resource_id":{"name":"app_arn","value":"arn:aws:elasticbeanstalk:us-east-1:802878444238:application/mcms-eb-test"},"provider_account":"802878444238","kpis":[{"kpi":"aws.elasticbeanstalk.health_status","value":"80","unit":"%","datetime":""}]}2019-07-05T18:17:08.257Z
*Expected*
new json='{"provider_code":"test","provider_resource_id":{"name":"test","value":"test"},"provider_account":"123","kpis":[{"kpi":"kpis","value":"80","unit":"%","datetime":"2019-07-05T18:17:08.257Z"}]}'
Your problem seems to be that you're writing json_input to newjson, when you really want to be writing decoded to newjson, as decoded is the object that you're modifying. json_input never gets modified, it is only used to create the JSON representation in Python. Furthermore, the +x['datetime'] doesn't help with anything, and is probably what's causing that added datetime at the end of your newjson.
Instead of newjson=json_input+x['datetime'], try newjson = decoded.dumps().
import json
json_input = '{"provider_code":"test","provider_resource_id":{"name":"test","value":"test"},"provider_account":"123","kpis":[{"kpi":"kpis","value":"80","unit":"%","datetime":""}]}'
try:
decoded = json.loads(json_input)
# Access data
for item in decoded['kpis']:
if not item['datetime']:
item['datetime'] = "2019-07-05T18:17:08.257Z"
except (ValueError, KeyError, TypeError):
print("JSON format error")
print(decoded)
I have this JSON in a file:
{"groupcolor":[
{"user":"group01", "color":"blue"},
{"user":"group02", "color":"yellow"},
{"user":"group03", "color":"green"}
]}
and I want to use Python(3) to verify if the content of "user" matches with "color". I've tried:
import json
with open('groupcolor.json') as f:
for line in f:
if f.user == group01 and f.color = blue:
print("ok!")
else:
print ("not ok")
but it obviously isn't the right syntax. most of the information that I found is focused on parsing or adding information, but I haven't found anything about checking the relation between two elements. is a way to do it in Python?
You definitely have the right idea: just the wrong syntax, as you point out.
As a comment suggests, you need to use json.load() (but not json.loads(), as json.loads() is for a string, not a file). This will rope in the json file as a dictionary.
import json
with open('groupcolor.json') as f:
json_dict = json.load(f)
users = json_dict["groupcolor"]
for item in users:
if item["user"] == "group01" and item["color"] == "blue":
print("ok!")
else:
print ("not ok")
Here is one solution:
import json
with open('groupcolor.json') as f:
group_color = json.load(f) # parse json into dict
group_color = group_color["groupcolor"] # get array out of dict
# create a dictionary where user is group01 and color is blue
search_criteria = dict(zip(("user", "color"), ("group01", "blue")))
for user_data in group_color:
message = "ok!" if user_data == search_criteria else "not ok"
print(message)
I want to show the content of an object using the following code:
def get(self):
url="https://www.googleapis.com/language/translate/v2?key=MY-BILLING-KEY&q=hello&source=en&target=ja"
data = urllib2.urlopen(url)
parse_data = json.load(data)
parsed_data = parse_data['data']['translations']
// This command is ok
self.response.out.write("<br>")
// This command shows above error
self.response.out.write(str(json.loads(parsed_data[u'data'][u'translations'][u'translatedText'])))
But the error
TypeError: expected string or buffer
appears as a result of the line:
self.response.out.write(str(json.loads(parsed_data[u'data'][u'translations'][u'translatedText'])))
or
self.response.out.write(json.loads(parsed_data[u'data'][u'translations'][u'translatedText']))
UPDATE (fix):
I needed to convert from string to JSON object:
# Convert to String
parsed_data = json.dumps(parsed_data)
# Convert to JSON Object
json_object = json.loads(parsed_data)
# Parse JSON Object
translatedObject = json_object[0]['translatedText']
# Output to page, by using HTML
self.response.out.write(translatedObject)
parse_data = json.load(data)
parsed_data = parse_data['data']['translations']
Those lines already did the json.load, and extracted 'data' and 'translations'. Then instead of:
self.response.out.write(str(
json.loads(parsed_data)[u'data'][u'translations'][u'translatedText']))
you should:
self.response.out.write(str(
parsed_data[u'translatedText']))
All I need, is convert from String to JSON object, as the following code :
# Convert to String
parsed_data = json.dumps(parsed_data)
# Convert to JSON Object
json_object = json.loads(parsed_data)
# Parse JSON Object
translatedObject = json_object[0]['translatedText']
# Output to page, by using HTML
self.response.out.write(translatedObject)
The urllib2.urlopen function return a file-like object, not a string. You should read the response first.
url = "http://www.example.com/data"
f = urllib2.urlopen(url)
data = f.read()
print json.loads(data)