Python json.loads () returns str on Heroku

Introduction

Heroku is convenient, isn't it? If you just move it for the time being, even a beginner can do it. This is a story I was addicted to when I tried to run Discord bot on Heroku with Heroku.

problem

Python's json.loads () is a function that converts a dictionary type (associative array) converted into a character string by json.dumps () etc. to the original dictionary type. Naturally, the return value is a dictionary type.

I was able to use it on local and GCP without any problem, but when I moved it on heroku in the same way, string type was returned for some reason. It's not a punch line that you accidentally used json.dumps ().

Heroku


##Json string.Unzip with loads
loaded_dict = json.loads(dumped_string)

##Output like that even if you print
print(loaded_dict)
# {"key1":"value1", "key2":{"v2k1":"v2v1"}}

##But it's a string
print(loaded_dict["key1"])
# TypeError: string indices must be integers
for k, v in loaded_dict.items():
    ...
# AttributeError: 'str' object has no attribute 'items'
print(type(loaded_dict["key1"]))
# <class 'str'>

Solutions

Heroku's search obstruction was so strong that I hit only articles like "I tried using Heroku" and couldn't find the root cause of the problem. However, the display of print (loaded_dict) itself was not strange, so I solved it with eval ().

Heroku


##Bite eval
loaded_dict = eval(json.loads(dumped_string))
##Solution!
print(loaded_dict["key1"])
# "value1"

Recommended Posts

Python json.loads () returns str on Heroku
python + django + scikit-learn + mecab (1) on heroku
python + django + scikit-learn + mecab (2) on heroku
Periodically run Python on Heroku Scheduler
My str (python)
Redis on Heroku
Python on Windows
twitter on python3
shimehari on heroku
LINE heroku python
Python on Windbg
A memo of a tutorial on running python on heroku
A story about running Python on PHP on Heroku
A memorandum for touching python Flask on heroku
Until you use PhantomJS with Python on Heroku
Install python on WSL
PyOpenGL setup on Python 3
Install Python on Pidora.
[Heroku] Memo for deploying Python apps using Heroku on Windows [Python]
Install Python on Mac
Install Python 3 on Mac
I tried python on heroku for the first time
Install Python3.4 on CentOS 6.6
Installing pandas on python2.6
I sent regular emails from sendgrid on heroku, on python
Install python on windows
Install Python 2.7.3 on CentOS 5.4
build Python on Ubuntu
Install Python 3.3 on Ubuntu 12.04
Install Python 3.4 on Mac
Install Python 3.6 on Docker
A memorandum of stumbling on my personal HEROKU & Python (Flask)
Don't lose to Ruby! How to run Python (Django) on Heroku
How to deploy the easiest python textbook pybot on Heroku
Run Openpose on Python (Windows)
Install Python 3.8 on RHEL 8 (AppStream)
Use matplotlib on Ubuntu 12 & Python
Install watchdog on Windows + Python 3.3
Install pygame on python3.4 on mac
Install Python 3.8 on CentOS 7 (SCL)
Real-time graphs on Plotly (Python)
Made python available on macOS
Deploy masonite app on Heroku 2020
Fastest Python installation on Windows
[Python] Notes on data analysis
Build Python environment on Windows
Run Tensorflow 2.x on Python 3.7
Handling of python on mac
Update python on Mac to 3.7-> 3.8
Install pandas 0.14 on python3.4 [on Mac]
Install OpenCV on Ubuntu + python
Notes on installing Python on Mac
Run Python CGI on CORESERVER
Twitter posts on Python 3 etc.
Run unix command on python
Build python environment on windows
Python --Install MySQLDB on EC2
Introducing TensorFlow on Ubuntu + Python 2.7
Install Python 3.8 on CentOS 8 (AppStream)
I ran python on windows
Broadcast on LINE using python