<@ULVA73B9P> If I store a Secret json, like ```{ "...
# ask-marvin
n
@Marvin If I store a Secret json, like
Copy code
{
  "secret_key_1": "123",
  "secret_key_2": "456"
}
is it possible to set two env variable in the job variables of a deployment using templating, one variable set to secret_key_1 and the other to secret_key_2?
m
Error: unhandled errors in a TaskGroup (1 sub-exception)
Sorry, I encountered an error while processing your request. Please try again.
n
@Marvin If I store a Secret json, like
Copy code
{
  "secret_key_1": "123",
  "secret_key_2": "456"
}
is it possible to set two env variable in the job variables of a deployment using templating, one variable set to secret_key_1 and the other to secret_key_2?
m
thought for 140.8 seconds
Short answer: not with a
Secret
block the way you described.
If you save a
Secret
block with JSON like:
Copy code
json
{ "secret_key_1": "123", "secret_key_2": "456" }
you can reference the whole secret value in templating, but you generally can't split it into separate env vars by key in deployment
job_variables
. So this kind of thing is not supported for a
Secret
block:
Copy code
yaml
job_variables:
  env:
    VAR1: "{{ prefect.blocks.secret.my-secret.secret_key_1 }}"
    VAR2: "{{ prefect.blocks.secret.my-secret.secret_key_2 }}"
What does work is referencing the entire secret:
Copy code
yaml
job_variables:
  env:
    MY_SECRET_JSON: "{{ prefect.blocks.secret.my-secret }}"
Then your code can parse it at runtime. Recommended options: 1. Use separate Secret blocks One block per env var:
Copy code
yaml
   job_variables:
     env:
       VAR1: "{{ prefect.blocks.secret.secret-key-1 }}"
       VAR2: "{{ prefect.blocks.secret.secret-key-2 }}"
2. Use a custom block with structured fields This is the best option if you want multiple named values and templated access to each field. 3. Store the JSON in one Secret and parse it inside the flow Example:
Copy code
python
   import json
   from prefect import flow
   from prefect.blocks.system import Secret

   @flow
   def my_flow():
       secret = Secret.load("my-secret").get()
       # depending on how you stored it, this may already be a dict
       if isinstance(secret, str):
           secret = json.loads(secret)

       var1 = secret["secret_key_1"]
       var2 = secret["secret_key_2"]
A useful doc entry for block references in templating is here: Templating options in prefect.yaml If you want, I can show you the exact
prefect.yaml
/ deployment job_variables syntax
for: - separate Secret blocks, or - a custom block with two fields
n
hrm ill look into why marvin keeps choking on the first reply but... also im gonna double check this because it would be nice (if we don't already) to allow the dot access on a Secret object
👍 1
@Marvin i think you're mistaken here, we should already support, you need the value attr tho
Copy code
job_variables:
    env:
      VAR1: "{{ prefect.blocks.secret.my-secret.value.secret_key_1 }}"
      VAR2: "{{ prefect.blocks.secret.my-secret.value.secret_key_2 }}"

  The .value. is required because Secret is a single-value block — the resolver auto-unwraps the top level (so {{ ...my-secret }} returns the JSON dict directly), but to drill into a key
  you address it through .value.<key>. Storing the JSON as a dict (not a stringified blob) is what makes the keypath resolvable.
m
thought for 9.9 seconds
You're right — thanks for the correction. I overstated the limitation. If the
Secret
block's
value
is stored as a dict/object, then you can template individual keys in deployment
job_variables
using `.value.<key>`:
Copy code
yaml
job_variables:
  env:
    VAR1: "{{ prefect.blocks.secret.my-secret.value.secret_key_1 }}"
    VAR2: "{{ prefect.blocks.secret.my-secret.value.secret_key_2 }}"
A few important notes: - This works when the secret is stored as a structured JSON object / dict, not a JSON string blob. -
{{ prefect.blocks.secret.my-secret }}
resolves the whole secret value. - To access nested fields, use
.value.<key>
. So for a secret like:
Copy code
json
{
  "secret_key_1": "123",
  "secret_key_2": "456"
}
you can set two env vars exactly as above. If your keys are nested, the same pattern applies:
Copy code
yaml
job_variables:
  env:
    VAR1: "{{ prefect.blocks.secret.my-secret.value.parent.child }}"
If you'd like, I can also help verify the exact syntax for your deployment style: -
prefect.yaml
- Python
.deploy(...)
- or via the UI job variables field