Python Forum
Beautify dictionary without converting to string.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beautify dictionary without converting to string.
#1
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)
buran write Apr-11-2021, 06:43 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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"
]
}
]
}
}
Reply
#4
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.
Reply
#5
(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
Reply
#6
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?
Reply
#7
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help converting string to int dedesssse 7 896 Jul-07-2021, 09:32 PM
Last Post: deanhystad
  how to deal with problem of converting string to int usthbstar 1 846 Jan-05-2021, 01:33 PM
Last Post: perfringo
  Converting data in CSV and TXT to dictionary kam_uk 3 810 Dec-22-2020, 08:43 PM
Last Post: bowlofred
  Converting string to hex triplet menator01 4 1,887 Aug-03-2020, 01:00 PM
Last Post: deanhystad
  extract a dictionary from a string berc 4 1,329 Jul-30-2020, 06:58 AM
Last Post: berc
  simple f-string expressions to access a dictionary Skaperen 0 766 Jul-15-2020, 05:04 AM
Last Post: Skaperen
  converting string object inside a list into an intiger bwdu 4 1,251 Mar-31-2020, 10:36 AM
Last Post: buran
  problem coverting string data file to dictionary AKNL 22 2,941 Mar-10-2020, 01:27 PM
Last Post: AKNL
  Converting query string as a condition for filter data. shah_entrance 1 876 Jan-14-2020, 09:22 AM
Last Post: perfringo
  Converting a patterned string or text into excel table soup1987 1 1,213 Oct-03-2019, 01:37 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020