Skip to main content
Added code to illustrate input string is a raw literal
Source Link
asterisk
  • 197
  • 3
  • 11

I have to format body of a json that gets some values substituted from user environment variables. Trying to find a way via shopt or something so the json string is substituted correctly in bash script but preserves/puts back the double quotes to be a valid json. Tried preprocessing quotes and double quoting.

Example:

$ export env_var1=val1
$ export env_var2=val2
$ echoexport args='{"key1": "${env_var1}", "key2": "${env_var2}"}'
$ echo $args #original input
$ eval echo $args #bad output- variables interpolated but quotes lost
Expected output= {"key1": "val1", "key2": "val2"}

Edit: My constraint here is I do not know the arbitrary json string in advance and I cannot reformat it by hand because the structure or names are not known to me in advance. I do not need to solve this specific case - its a toy example. I am trying to formulate a generic solution for an arbitrary string of which the example is a representative. The keys , values or both could be env variables. The json could be arbitrarily nested and large.

I have to format body of a json that gets some values substituted from user environment variables. Trying to find a way via shopt or something so the json string is substituted correctly in bash script but preserves/puts back the double quotes to be a valid json. Tried preprocessing quotes and double quoting.

Example:

$ export env_var1=val1
$ export env_var2=val2
$ echo {"key1": "${env_var1}", "key2": "${env_var2}"}
Expected output= {"key1": "val1", "key2": "val2"}

Edit: My constraint here is I do not know the arbitrary json string in advance and I cannot reformat it by hand because the structure or names are not known to me in advance. I do not need to solve this specific case - its a toy example. I am trying to formulate a generic solution for an arbitrary string of which the example is a representative. The keys , values or both could be env variables. The json could be arbitrarily nested and large.

I have to format body of a json that gets some values substituted from user environment variables. Trying to find a way via shopt or something so the json string is substituted correctly in bash script but preserves/puts back the double quotes to be a valid json. Tried preprocessing quotes and double quoting.

Example:

$ export env_var1=val1
$ export env_var2=val2
$ export args='{"key1": "${env_var1}", "key2": "${env_var2}"}'
$ echo $args #original input
$ eval echo $args #bad output- variables interpolated but quotes lost
Expected output= {"key1": "val1", "key2": "val2"}

Edit: My constraint here is I do not know the arbitrary json string in advance and I cannot reformat it by hand because the structure or names are not known to me in advance. I do not need to solve this specific case - its a toy example. I am trying to formulate a generic solution for an arbitrary string of which the example is a representative. The keys , values or both could be env variables. The json could be arbitrarily nested and large.

added 273 characters in body
Source Link
asterisk
  • 197
  • 3
  • 11

I have to format body of a json that gets some values substituted from user environment variables. Trying to find a way via shopt or something so the json string is substituted correctly in bash script but preserves/puts back the double quotes to be a valid json. Tried preprocessing quotes and double quoting.

Example:

$ export env_var1=val1
$ export env_var2=val2
$ echo {"key1": "${env_var1}", "key2": "${env_var2}"}
Expected output= {"key1": "val1", "key2": "val2"}

Edit: My constraint here is I do not know the arbitrary json string in advance and I cannot reformat it by hand because the structure or names are not known to me in advance. I do not need to solve this specific case - its a toy example. I am trying to formulate a generic solution for an arbitrary string of which the example is a representative. The keys , values or both could be env variables. The json could be arbitrarily nested and large.

I have to format body of a json that gets some values substituted from user environment variables. Trying to find a way via shopt or something so the json string is substituted correctly in bash script but preserves/puts back the double quotes to be a valid json. Tried preprocessing quotes and double quoting.

Example:

$ export env_var1=val1
$ export env_var2=val2
$ echo {"key1": "${env_var1}", "key2": "${env_var2}"}
Expected output= {"key1": "val1", "key2": "val2"}

Edit: My constraint here is I do not know the arbitrary json string in advance and I cannot reformat it by hand because the structure or names are not known to me in advance.

I have to format body of a json that gets some values substituted from user environment variables. Trying to find a way via shopt or something so the json string is substituted correctly in bash script but preserves/puts back the double quotes to be a valid json. Tried preprocessing quotes and double quoting.

Example:

$ export env_var1=val1
$ export env_var2=val2
$ echo {"key1": "${env_var1}", "key2": "${env_var2}"}
Expected output= {"key1": "val1", "key2": "val2"}

Edit: My constraint here is I do not know the arbitrary json string in advance and I cannot reformat it by hand because the structure or names are not known to me in advance. I do not need to solve this specific case - its a toy example. I am trying to formulate a generic solution for an arbitrary string of which the example is a representative. The keys , values or both could be env variables. The json could be arbitrarily nested and large.

Added constraint ( unknown string , cannot restructure based on knowledge about the names)
Source Link
asterisk
  • 197
  • 3
  • 11

I have to format body of a json that gets some values substituted from user environment variables. Trying to find a way via shopt or something so the json string is substituted correctly in bash script but preserves/puts back the double quotes to be a valid json. Tried preprocessing quotes and double quoting.

Example:

$ export env_var1=val1
$ export env_var2=val2
$ echo {"key1": "${env_var1}", "key2": "${env_var2}"}
Expected output= {"key1": "val1", "key2": "val2"}

Edit: My constraint here is I do not know the arbitrary json string in advance and I cannot reformat it by hand because the structure or names are not known to me in advance.

I have to format body of a json that gets some values substituted from user environment variables. Trying to find a way via shopt or something so the json string is substituted correctly in bash script but preserves/puts back the double quotes to be a valid json. Tried preprocessing quotes and double quoting.

Example:

$ export env_var1=val1
$ export env_var2=val2
$ echo {"key1": "${env_var1}", "key2": "${env_var2}"}
Expected output= {"key1": "val1", "key2": "val2"}

I have to format body of a json that gets some values substituted from user environment variables. Trying to find a way via shopt or something so the json string is substituted correctly in bash script but preserves/puts back the double quotes to be a valid json. Tried preprocessing quotes and double quoting.

Example:

$ export env_var1=val1
$ export env_var2=val2
$ echo {"key1": "${env_var1}", "key2": "${env_var2}"}
Expected output= {"key1": "val1", "key2": "val2"}

Edit: My constraint here is I do not know the arbitrary json string in advance and I cannot reformat it by hand because the structure or names are not known to me in advance.

Fixed to use correct double quotes typeface
Source Link
asterisk
  • 197
  • 3
  • 11
Loading
Edited shell prompt to $ from %
Source Link
asterisk
  • 197
  • 3
  • 11
Loading
Source Link
asterisk
  • 197
  • 3
  • 11
Loading