Search code examples
pythonjsonenviron

How do I obtain a raw json string from an environment variable in python?


In BASH, I can store JSON into an environment variable via jq:

export FOO=$(curl ... | jq -Rs)

Now, if I load that into python:

bar=os.environ["FOO"]

bar will have something like: {\\"this\\":\\"that\\"}, which is a problem when loading with json:

json.loads(bar) # error

I have tried a few things including repr, rf'{os.environ["FOO"]}' and so on, but there doesn't seem to be an internally managed way to drop the extra slashes.

How do I drop the extra slashes via string functions? I'd prefer not to simply replace them with a single slash, as I might have to touch that code again sometime in the future.


Solution

  • Add the -r flag to output as raw text from jq. Here is an example using echo that gives the escaped results:

    echo '{"fruit":{"name":"apple","color":"green","price":1.20}}' | jq -Rs
    # gives:
    "{\"fruit\":{\"name\":\"apple\",\"color\":\"green\",\"price\":1.20}}\n"
    

    But adding -r gives a result that is still in JSON format without all of the escaped quotes:

    echo '{"fruit":{"name":"apple","color":"green","price":1.20}}' | jq -Rsr
    # gives:
    {"fruit":{"name":"apple","color":"green","price":1.20}}
    

    Which is parseable by Python.

    export FOO=$(echo '{"fruit":{"name":"apple","color":"green","price":1.20}}' | jq -Rsr)
    
    import os
    import json
    
    json.loads(os.environ['FOO'])
    # returns:
    # {'fruit': {'name': 'apple', 'color': 'green', 'price': 1.2}}