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)
Related
I pass the following json via command prompt as
$python new.py {'scenarioId':'null','scenarioName':'EC_02','scenarioDesc':'EC_02','riskEngine':'null'}
and when I run the following:
import sys
import json
str_json = sys.argv[1].replace("'", '"')
try:
d = json.dumps(str_json)
dat = json.loads(b)
print("Stress JSON is as follows \n",dat)
except json.decoder.JSONDecodeError:
print("Not a valid JSON")
print(dat['scenarioId'])
It's a valid json but I get the error TypeError: string indices must be integers for the last line.
It works fine when I initialize it as str_json in the code itself.
How to solve this?
In your code when you are using a_json = json.loads(b), here type of b variable is still string, convert it into json: a_json = json.loads(json.loads(b))
Code:
import sys
import json
str_json = sys.argv[1].replace("'", '"')
try:
b = json.dumps(str_json)
a_json = json.loads(json.loads(b))
print("Stress JSON is as follows \n",a_json)
except json.decoder.JSONDecodeError:
print("Not a valid JSON")
print(a_json['scenarioId'])
Output:
Stress JSON is as follows
{'scenarioId': 'null', 'scenarioName': 'EC_02', 'scenarioDesc': 'EC_02', 'riskEngine': 'null'}
null
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
I'm not sure why this happen, there are zero length in my json file.
0
I t supposedly to be like this,
1000
I'm afraid the comma thing after each json object cause this issue. (My current json format)
{ A:"A"},{ B:"B"),...
The correct way is like this
{ A:"A"} { B:"B"),...
So how I can calculate all the length without removing the comma?
My code
import json
githubusers_data_path = 'githubusers.json'
githubusers_data = []
githubusers_file = open(githubusers_data_path, "r")
for line in githubusers_file:
try:
data = json.loads(line)
githubusers_data.append(data)
except:
continue
print len(githubusers_data)
Sample
{
"login": "datomnurdin"
}, {
"login": "ejamesc"
},...
I think you're getting an exception that you're suppressing with try-except, because of the commas.
One solution would be to convert your file to a string first, stick a '[' and ']' around the string to convert it into a valid json format, then use json.loads to convert the string.
import json
githubusers_data_path = 'githubusers.json'
githubusers_file = open(githubusers_data_path, "r")
githubusers_string = ''.join(line for line in githubusers_file)
githubusers_string = '[{}]'.format(githubusers_string)
githubusers_data = json.loads(githubusers_string)
print len(githubusers_data)
githubusers_file.close()
there is an exception in your code:
import json
githubusers_data_path = 'githubusers.json'
githubusers_data = []
githubusers_file = open(githubusers_data_path, "r")
for line in githubusers_file:
try:
data = json.load(githubusers_file) # exception from here
githubusers_data.append(data)
except Exception, e:
print e
print len(githubusers_data) # so githubusers_data is always []
Mixing iteration and read methods would lose data
I've got some data I've acquired from the Twitter Streaming API that I'm trying to parse using python. I've been successful doing this for the most part when parsing out objects that are in ALL records. However, when trying to parse objects that are not be in every record, such as ['coordinates'] or ['entities']['hashtags'] I run into an error.
import json
import sys
def main():
for line in sys.stdin:
line = line.strip()
data = ''
try:
data = json.loads(line)
except ValueError as detail:
continue
if not (isinstance(data, dict)):
## not a dictionary, skip
pass
elif 'delete' in data:
## a delete element, skip for now.
pass
elif 'user' not in data:
## bizarre userless edge case
pass
else:
print "\t".join([
data['created_at'],
data['user']['screen_name'],
data['user']['id_str'],
data['user']['lang'],
data['text'],
data['source']
]).encode('utf-8')
if __name__ == '__main__':
main()
Rather than using data['coordinates'], you could use data.get('coordinates').
Using dict.get(key) will return None if the key isn't in the dictionary, rather than raising a KeyError.
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)