Convert environment variables into a json file
The following assumes the "environment variable" names and values could be made available as variable=value strings:
function data {
cat <<EOF
Security:ClientId=123456
Security:clientSecret=abcdefg
AppSettings:Environment=Dev
EOF
}
data | jq -nR '
def parse: capture("(?<x>[^:]*):(?<y>[^=]*)=(?<value>.*)");
reduce inputs as $line ({};
($line | parse) as $p
| .[$p.x][$p.y] = ($p.value) )
'
Output
As shown in the Q.
How to use environment variables within a command in jq script files?
You are dynamically constructing a filter using the value of a variable. This is fragile, as the resulting expression is parsed, rather than using the value of the variable literally (similar to SQL injection attacks).
You can access the environment directly using $ENV
.
% export entity_name=my_entity_name
% jq -r 'select(.components.schemas[$ENV.entity_name].properties != null)' file.json
or simply pass the key as an explicit argument
% jq -r --arg entity_name my_entity_name 'select(.components.schemas[$entity_name].properties != null)' file.json
How can I convert JSON to an environment variable file format?
Easy with jq
and some string interpolation and @sh
to properly quote the Value strings for shells:
$ jq -r '.Parameters[] | "\(.Name | .[rindex("/")+1:])=\(.Value | @sh)"' input.json
CORS_ORIGIN='https://example.com'
DATABASE_URL='db://user:pass@host:3306/example'
Export environment variables to JSON in Bash
JQ does that for you and populates an internal variable called ENV
with the result, which can be stored in a shell variable like so:
var=$(jq -n '$ENV')
And to remove junk variables such as _
, SHLVL
, etc. from the list, you can use JQ's del
function.
var=$(jq -n '$ENV | del(._, .SHLVL)')
How can we store a JSON credential to ENV variable in python?
Assuming your JSON file is creds.json
creds.json
{
"type": "service_account",
"project_id": "project_id",
"private_key_id": "private_key_id",
"private_key": "-----BEGIN PRIVATE KEY-----\n",
"client_email": "email",
"client_id": "id",
"auth_uri": "uri_auth",
"token_uri": "token_urin",
"auth_provider_x509_cert_url": "auth_provider_x509_cert_url",
"client_x509_cert_url": "client_x509_cert_url"
}
main.py
import json
data = json.load(open('creds.json'))
f = open(".env", "x")
for key, value in data.items():
f.write(f"{key.upper()}={value}\n")
creds.env will be generated
TYPE=service_account
PROJECT_ID=project_id
PRIVATE_KEY_ID=private_key_id
PRIVATE_KEY=-----BEGIN PRIVATE KEY-----
CLIENT_EMAIL=email
CLIENT_ID=id
AUTH_URI=uri_auth
TOKEN_URI=token_urin
AUTH_PROVIDER_X509_CERT_URL=auth_provider_x509_cert_url
CLIENT_X509_CERT_URL=client_x509_cert_url
create_keyfile_dict()
basically returns a dict called variable_keys
from dotenv import load_dotenv
load_dotenv()
def create_keyfile_dict():
variables_keys = {
"type": os.getenv("TYPE"),
"project_id": os.getenv("PROJECT_ID"),
"private_key_id": os.getenv("PRIVATE_KEY_ID"),
"private_key": os.getenv("PRIVATE_KEY"),
"client_email": os.getenv("CLIENT_EMAIL"),
"client_id": os.getenv("CLIENT_ID"),
"auth_uri": os.getenv("AUTH_URI"),
"token_uri": os.getenv("TOKEN_URI"),
"auth_provider_x509_cert_url": os.getenv("AUTH_PROVIDER_X509_CERT_URL"),
"client_x509_cert_url": os.getenv("CLIENT_X509_CERT_URL")
}
return variables_keys
scope=['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/spreadsheets'
]
credentials = ServiceAccountCredentials.from_json_keyfile_name(create_keyfile_dict(), scope)
client = gspread.authorize(credentials)
Related Topics
Why Does Script Not Recognize File Extension
How to Read from Text File Line-By-Line and Split the Line by a Character
Replace Text Between Two Lines with Contents of a File Stored in a Variable in Sed
Bash/Linux Sort by 3Rd Column Using Custom Field Seperator
Compute Base64 Encoded Hash from a Given Hash
Why Count Differs Between Ls and Ls -L Linux Command
Stop on Newline When Using Read(...)
G++ Always Fails with Undefined Reference to _Unwind_Getipinfo
Simple Way to Get Filesize in X86 Assembly Language
Safely Remembering Ssh Credentials in Bash Script
How to Mail Script Output in Table Format
How to Escape the Bang (!) Character in Linux Bash Shell
Use Bash Variable Within Slurm Sbatch Script
How to Delete Files Over (N) Days Old But Leave (N) Files Regardless of Age