<@ULVA73B9P> How can I store and read value of a j...
# ask-marvin
e
@Marvin How can I store and read value of a json in a prefect variable ? I'm in version 3.0.9. Case 1 : only a string : it does work : Variable.get("test") 222245.261 | INFO | Task run 'mytask' - Variable(id=UUID('6f162b92-b199-4ed8-bdf2-0f8882c519bc'), name='test', value='test', tags=[]) Case 2 : a json containing {"test":"Hello world"} : it does not work Variable.get("test") 222320.015 | INFO | Task run 'mytask' - * pydantic.v1.error_wrappers.ValidationError: 1 validation error for Variable value str type expected (type=type_error.str)
m
In Prefect 3.0.9, you can store and read JSON data in variables, but it appears that the JSON value needs to be a string. From the Prefect documentation, variables are described as dynamically-named, mutable string values, but they are also mentioned to be of any JSON type. This means that while the primary focus is on string data, you can still work with JSON structures by ensuring JSON data is serialized and deserialized appropriately. Here's how you can handle JSON data with Prefect variables: 1. Store JSON in a Prefect Variable as a String: - When storing a JSON object, first convert it to a string using
json.dumps()
. - Example:
Copy code
python
     import json
     from prefect import Variables

     my_json = {"test": "Hello world"}
     serialized_json = json.dumps(my_json)
     Variables.set(name="test", value=serialized_json)
2. Retrieve and Deserialize JSON from a Prefect Variable: - When retrieving the variable, convert the string back into a JSON object using
json.loads()
. - Example:
Copy code
python
     import json
     from prefect import Variables

     serialized_json = Variables.get("test")
     my_json = json.loads(serialized_json)
     print(my_json)  # Outputs: {'test': 'Hello world'}
By storing JSON data as a string, you ensure compatibility with the Prefect variable system, while still maintaining structured data that can be easily deserialized back into Python objects. You can find more details about variables in Prefect 3.x on this documentation page.