Python Forum
Beautify dictionary without converting to string. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Beautify dictionary without converting to string. (/thread-33262.html)



Beautify dictionary without converting to string. - sharoon - Apr-11-2021

I m trying to beatuify dicitonary using json.dumps , but as soon as it beautify , the data type changes from dictionary to string. Chnage of datatype to string not allowing it to post on slack channel.

Error while posting it on slack - Str indices must be integer. Point is either I ensure while beatufying data type dont changes or modificaiton be to done at slack configuraton end.


input = {"checkType":"employeePurchase_not_true","output":[{"purchaseType":"ORDER","recordCount":["AU: 1","FR: 1","IT: 11","BE: 1","AT: 1","SE: 1","ES: 1","FI: 1","NL: 1","PT: 1","NO: 1","DE: 2","PL: 1","SI: 1","IE: 1","GB: 7","DK: 1","US: 1","SK: 1","RU: 2","CA: 1","CH: 1"]},{"purchaseType":"RECEIPT","recordCount":["AU: 956","FR: 1","IT: 5","BE: 165","AT: 2","SE: 1","ES: 5765","FI: 1","NL: 2","CZ: 308","HU: 1","KR: 2","NO: 3","IN: 3","DE: 45","PL: 1","SI: 1","JP: 3","GB: 2","RS: 1","DK: 5","US: 762","RU: 442","RO: 1","HR: 13","CA: 3","CH: 36"]}]}
slack_msg["channel"] = "ce-ecd-ops-dataquality"
slack_msg["message"] = input
d=json.dumps(slack_msg,indent=4)    
send_slack_alert(d)



RE: Beautify dictionary without converting to string. - buran - Apr-11-2021

First of all "beautify" refers only to representation of the dict (or any other container) when printed as output or written to file (e.g. in case of json).
Second, don't use input as name - it is built-in function.
Third, post the full traceback you get and ideally minimal reproducible example - in my opinion the error message indicates the problem is not what you think.


RE: Beautify dictionary without converting to string. - sharoon - Apr-11-2021

(Apr-11-2021, 06:48 AM)buran Wrote: First of all "beautify" refers only to representation of the dict (or any other container) when printed as output or written to file (e.g. in case of json).
Second, don't use input as name - it is built-in function.
Third, post the full traceback you get and ideally minimal reproducible example - in my opinion the error message indicates the problem is not what you think.
===================
This how it get beautifies using json .dump but the problem is its get chnages to string. we wwant to retain it as dicitonary
{
"channel": "ce-ecd-ops-dataquality",
"message": {
"checkType": "employeePurchase_not_true",
"output": [
{
"purchaseType": "ORDER",
"recordCount": [
"AU: 1",
"FR: 1",
"IT: 11",
"BE: 1",
"AT: 1",
"SE: 1",
"ES: 1",
"FI: 1",
"NL: 1",
"PT: 1",
"NO: 1",
"DE: 2",
"PL: 1",
"SI: 1",
"IE: 1",
"GB: 7",
"DK: 1",
"US: 1",
"SK: 1",
"RU: 2",
"CA: 1",
"CH: 1"
]
},
{
"purchaseType": "RECEIPT",
"recordCount": [
"AU: 956",
"FR: 1",
"IT: 5",
"BE: 165",
"AT: 2",
"SE: 1",
"ES: 5765",
"FI: 1",
"NL: 2",
"CZ: 308",
"HU: 1",
"KR: 2",
"NO: 3",
"IN: 3",
"DE: 45",
"PL: 1",
"SI: 1",
"JP: 3",
"GB: 2",
"RS: 1",
"DK: 5",
"US: 762",
"RU: 442",
"RO: 1",
"HR: 13",
"CA: 3",
"CH: 36"
]
}
]
}
}


RE: Beautify dictionary without converting to string. - ibreeden - Apr-11-2021

But what do you mean? Do you mean the list input["output"]["recordCount"] ? It is a list of strings. But it was also a list of strings on input.


RE: Beautify dictionary without converting to string. - sharoon - Apr-11-2021

(Apr-11-2021, 07:26 AM)ibreeden Wrote: But what do you mean? Do you mean the list input["output"]["recordCount"] ? It is a list of strings. But it was also a list of strings on input.

============
The whole body of dictionary get enclosed in "" after using json.dumps


RE: Beautify dictionary without converting to string. - bowlofred - Apr-11-2021

json.dumps turns a dictionary into a string. If you want to continue using it as a dictionary object, why are you using json.dumps at all?


RE: Beautify dictionary without converting to string. - buran - Apr-11-2021

I am asking for the full traceback. The error Str indices must be integer indicates there is something more going on and not what you think.