There is an incoming json(data.json)
:
{ "data": [ { "Name": "Ivan", "parameter1": "XXX", "parameter2": 0, "parameter3": 0, "date": "2016-07-12T00:00:00", "parameter4": 2 }, { "Name": "Ivan", "parameter1": "YYY", "parameter2": 0, "parameter3": 0, "date": "2016-07-12T00:00:00", "parameter4": 2 }, { "Name": "Oleg", "parameter1": "XXX", "parameter2": 2, "parameter3": 3, "date": "2016-07-12T00:00:00", "parameter4": 2 }, { "Name": "Oleg", "parameter1": "YYY", "parameter2": 4, "parameter3": 3, "date": "2016-07-12T00:00:00", "parameter4": 2 } ] }
The task is to parse this json(attachments.json)
, substitute the values into another json
for the Slack
messenger, then send it with a message of the following type:
[ { "fallback": "Required plain-text summary of the attachment.", "color": "good", "pretext": "мяу" }, { "author_name": "Ivan", "author_icon": "http://unicodey.com/emoji-data/img-twitter-64/1f4e2.png", "fields": [ { "title": "parameter1", "value": "XXX", "short": "true" }, { "title": "parameter2", "value": "0", "short": "true" }, { "title": "parameter3", "value": "0", "short": "true" }, { "title": "parameter4", "value": "2", "short": "true" }, { "title": "parameter1", "value": "YYY", "short": "true" }, { "title": "parameter2", "value": "0", "short": "true" }, { "title": "parameter3", "value": "0", "short": "true" }, { "title": "parameter4", "value": "2", "short": "true" }, ] }, { "author_name": "Oleg", "author_icon": "http://unicodey.com/emoji-data/img-twitter-64/1f4e2.png", "fields": [ { "title": "parameter1", "value": "XXX", "short": "true" }, { "title": "parameter2", "value": "0", "short": "true" }, { "title": "parameter3", "value": "0", "short": "true" }, { "title": "parameter4", "value": "2", "short": "true" }, { "title": "parameter1", "value": "YYY", "short": "true" }, { "title": "parameter2", "value": "0", "short": "true" }, { "title": "parameter3", "value": "0", "short": "true" }, { "title": "parameter4", "value": "2", "short": "true" }, ] },
My current solution is:
create a dictionary
Ivan = dict(xxx={'s': 0, 'c': 0, 'a': 0,}, yyy={'s': 0, 'c': 0, 'a': 0,})
Parse the first
json
function and write the necessary values to the dictionarydef get_data(): with open('data.json', 'r') as data: jsonsheet = json.load(data) jsondata = jsonsheet["data"] for i in jsondata: if i['parameter1'] == 'XXX': if i['Name'] == 'Ivan': Ivan['xxx']['s'] = i['parameter2'] Ivan['xxx']['c'] = i['parameter3'] Ivan['xxx']['a'] = i['parameter4'] elif i['parameter2'] == 'YYY': Ivan['yyy']['s'] = i['parameter2'] Ivan['yyy']['c'] = i['parameter3'] Ivan['yyy']['a'] = i['parameter4']
the function loads
json(attachments.json)
takes the necessary values from the dictionary (Ivan
) and overwrites the data and returnsjson
.def load_data(): with open('attachments.json', 'r', encoding='utf-8') as msg: msgdata = json.load(msg) jsonmsg = msgdata[1] jsonmsg['fields'][2]['value'] = Ivan['parameter2']['s'] return json.dumps(msgdata, ensure_ascii=False)
Questions:
If I call
print(load_data())
, it will returnjson
with modified values, however, when I send a message to the chat, then an unchangedjson
is given. Why is that?In the incoming json there may be other names besides Ivan with the same parameters. I suppose that it is possible to create a class for such a case, but I don’t understand how to bind it to the
get_data()
function?Is there a more elegant solution instead of
get_data()
?- The code seems extremely large. Maybe someone has ideas on refactoring?
Addition: Instead of a dictionary I decided to use a class
class Salesmanager: def __init__(self, n): self.name = n self.xxx = ['xxx',{'sum': 0, 'chq': 0, 'avg': 0}] self.yyy = ['yyy',{'sum': 0, 'chq': 0, 'avg': 0}] def setname(self, newname): self.name = newname def set_values_d(self, s, c, a): self.xxx[1]['sum'] = s self.xxx[1]['chq'] = c self.xxx[1]['avg'] = a def set_values_nd(self, s, c, a): self.yyy[1]['sum'] = s self.yyy[1]['chq'] = c self.yyy[1]['avg'] = a def __str__(self): return 'Имя: {0}\n''xxx: {1}\n''yyy: {2}'. \ format(self.name, self.xxx, self.yyy)