I have a task to upload MySQL table JSON data to the rest API. I am loading the data in the Pandas dataframe and transforming it and trying to make a single JSON value to post to the API endpoint.
The task is to collect all rows of MySQL table json column data and embeds it into a single JSON data to post all rows of JSON data in a single post request of Python.
Mysql table rows data example value as below –
id | source | action | object_pk | json_data |
---|---|---|---|---|
1 | A | Update | 701 | [{"keys": {"PlayerID": "701"}, "values": {"EMail": "[email protected]", "Active": "activenotverified", "Gender": "male", "Street": "kolol", "Balance": 2000, "BrandId": 1, "Country": "US", "Currency": "USD", "Language": "en"}}] |
2 | A | Update | 702 | [{"keys": {"PlayerID": "702"}, "values": {"EMail": "[email protected]", "Active": "activenotverified", "Gender": "Female", "Street": "Abc", "Balance": 500, "BrandId": 2, "Country": "GR", "Currency": "EURO", "Language": "gr"}}] |
After loading, this data into the pandas dataframe would like to merge all rows of JSON data into a single valid JSON data.
Trying for expected output as below in Pandas df –
json_data |
---|
[{"keys": {"PlayerID": "701"}, "values": {"EMail": "[email protected]", "Active": "activenotverified", "Gender": "male", "Street": "kolol", "Balance": 2000, "BrandId": 1, "Country": "US", "Currency": "USD", "Language": "en"}},{"keys": {"PlayerID": "702"}, "values": {"EMail": "[email protected]", "Active": "activenotverified", "Gender": "male", "Street": "kolol", "Balance": 2000, "BrandId": 1, "Country": "US", "Currency": "USD", "Language": "en"}}] |
My goal is to load mysql table data every 10 minutes interval and send all rows of JSON data in a single post to the rest API endpoint.
r = requests.post("https://abc/rowset", json=json_data,headers=headers)
Not sure how to accomplish this in Pandas. Any pointers or code samples will be highly appreciated! Thanks in advance
2
Answers
According to your last comment, the content of the
json_data
column is a (json) string representation of a list containing one single element which is a dict. IFAIK, there is no way to directly convert that column to a single json.A possible way is to:
It could be implemented as:
with your shown data, I got:
Not sure if I understood you correctly, but you can try something like this:
This should return a
dict
which is technically a JSON-looking file, where each row index will be the key & wholedf['json_data']
column as value, i.e.:etc.
Not sure why each
json_data
row is in[]
brackets, maybe you would want to remove them from with the help ofsubstr
or any similar method.more about
to_dict()
method