Hi everyone - How to convert prefect python dict p...
# prefect-community
v
Hi everyone - How to convert prefect python dict parameter to JSON string? I am looking at JSONCompatible. Please point me to some examples.
j
Hi Vinod, can you expand on what you're trying to do? It sounds like you're trying to use a
Parameter
with a dict-default (which should work fine)? You should never need to touch internal things like
JSONCompatible
. Can you post an example of what you're trying to accomplish, and what errors you're getting?
v
Hi Jim, yes, I am using a parameter with a dict, and want to pass this parameter to POST REST call as a data. For this scenario, I need to covert dict to JSON string. I will share the snippet in sometime.
c
Hi @Vinod Sugur - this is a pure Python question unrelated to Prefect or Parameters; to pass a dictionary to a POST request with Python, there are many options but the simplest is to use `requests`:
Copy code
import requests
import json

my_dict = {"x": 42}

<http://requests.post|requests.post>(url, json=my_dict) # post accepts a json kwarg that accepts dictionaries

<http://requests.post|requests.post>(url, data=json.dumps(my_dict)) # or you can convert dict to JSON string using the json library
v
No @Chris White, I am using curl command in shelltask which uses the parameter in dict as part of POST data, and hence I need to convert the parameter to JSON string.
👍 1
@Chris White , issue resolved
👍 1