I am creating a CDK stack using python.
Here I am exporting json object in to a linux environment as it is a clodebuild step.
f"export SHARED="{json.dumps(shared)}""
The only reason to use "
is i was getting an error for spaces with in the json object.
When I am trying to import environment object and load it as json i am getting json object without ""
.
{
mts:{
account_id:11111,
workbench:aaaaa,
prefix:rad600-ars-sil,rad600-srr-sil-stage1,rad600-srr-sil-stage2
},
tsf:{
account_id:22222,
workbench:bbbbb,
prefix:yyyy
}
}
with this object below loads is not working and giving out an error which states json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes
SHARED = json.loads(os.environ["SHARED"])
Am I missing something or is there a better way to send json object as environment variable?
3
Answers
This is silly, but this worked.
I don't exactly know how it worked internally but replacing
with
worked!
I feel while accessing the variable
os.environ["SHARED"]
it was nullifying""
as the entire object had""
within itself and by adding'
this surrounded by object with"
it was able to distinguish better.Just a theory!!!
Using base64
One foolproof way is to encode your json string as base64 first.
Then on the other end:
This technique also works for arbitrary binary data.
Using shlex.quote
Another way to do this would be to use
shlex.quote
to properly quote your json string for the shell command.