Vinod Sugur

11/25/2020, 7:26 PM
Hi everyone - How to convert prefect python dict parameter to JSON string? I am looking at JSONCompatible. Please point me to some examples.

Jim Crist-Harif

11/25/2020, 8:14 PM
Hi Vinod, can you expand on what you're trying to do? It sounds like you're trying to use a
with a dict-default (which should work fine)? You should never need to touch internal things like
. Can you post an example of what you're trying to accomplish, and what errors you're getting?

Vinod Sugur

11/26/2020, 1:19 AM
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.

Chris White

11/26/2020, 5:40 AM
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}

<|>(url, json=my_dict) # post accepts a json kwarg that accepts dictionaries

<|>(url, data=json.dumps(my_dict)) # or you can convert dict to JSON string using the json library

Vinod Sugur

11/26/2020, 9:11 AM
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