1

I am getting below output after executing a script, which I store in a variable

{
  "VariaB": "DONE",
  "VariaC": "DONE",
  "VariaD": null,
  "VariaE": true
}

I have another variable VariaA="ABCD" & VariaF which contains value true or false and want to insert in variable value. I want to print the final variable in below format

{
  "VariaA": "ABCD",
  "VariaB": "DONE",
  "VariaC": "DONE",
  "VariaD": null,
  "VariaE": true,
  "VariaF": false
}
3
  • possible duplicate: stackoverflow.com/questions/1955505/… Commented Sep 3, 2019 at 8:03
  • Is VariaA always a plain string? i.e. no array, object, number or boolean? Commented Sep 3, 2019 at 9:47
  • 3
    If your production servers manipulates JSON data, they must have some JSON aware tools in some way or another. Be it Python, PHP, NodeJS..., then maybe Bash/shell is not the right choice, because Bash alone or standard POSIX shell commands are not designated to manipulate JSON, unless you install specialized command-line tools like jq. Either use the others already available JSON aware tools on your production servers, or just add jq as a requirement in the deployment specs of your program. Commented Sep 3, 2019 at 10:55

3 Answers 3

1

As others said, please do your best to use a JSON-aware tool rather than basic string manipulation, it would be bound to save yourself some effort in the future if not some trouble.

Since you said you currently can't, here's a string manipulation "solution" :

printf "{
  \"VariaA\": \"$VariaA\",
%s
  \"VariaF\": $VariaF
}" "$(grep -v '[{}]' <<< "$input")"

printf handles the whole structure, and takes as parameter the middle block that is from your input. To get that middle block, we use grep to exclude the lines that contain brackets.

Note that this will fail in a lot of cases such as the input not being formatted as usual (a one liner would be proper JSON, but would make that script fail) or suddenly containing nested objects, the variables containing double-quotes, etc.

You can try it here.

Sign up to request clarification or add additional context in comments.

Comments

1

This pipeline does what you need

echo "{"`echo $VariaA | sed "s/{\|}//g"`,`echo "   "$VariaF | sed "s/{\|}//g"`"}" | sed "s/,/,\n/g" | sed "s/{/{\n/" | sed "s/}/\n}/" | uniq 

2 Comments

Thank you for this pipeline, but unfortunately it does not work if VariaF's value is a list. This is my case...
The user asks help to find a solution a for a specific case. All we can do is to help giving solutions that work in that case, of course they are not universal
0

Looks yout output is JSON, for appending to your output object you could use jq for example try this:

cat json.txt | jq --arg VariaA  ABCD '. + {VariaA: $VariaA}'

In this case, if json.txt contains your input:

{
  "VariaB": "DONE",
  "VariaC": "DONE",
  "VariaD": null,
  "VariaE": true
}

By using jq --arg VariaA ABCD '. + {VariaA: $VariaA}' it will then output:

{
  "VariaB": "DONE",
  "VariaC": "DONE",
  "VariaD": null,
  "VariaE": true,
  "VariaA": "ABCD"
}

If you would like to use more variables you need to use --arg multiple times, for example:

 jq --arg VariaA ABCD --argjson VariaX true '. + {VariaA: $VariaA, VariaX: $VariaX}'

The output will be:

{
  "VariaB": "DONE",
  "VariaC": "DONE",
  "VariaD": null,
  "VariaE": true,
  "VariaA": "ABCD",
  "VariaX": "true"
}

In this example cat json.txt simulates your command output but worth mention that if you wanted to process an existing file you could use (notice the <):

jq --arg VariaA  ABCD '. + {VariaA: $VariaA}' < file.txt

By doing this you do all in one single process.

9 Comments

thanks for the solution, can we do this without jq as its not there in our prod env.
I would highly suggest using jq or another JSON processor
you are correct this looks very clean but the constraint doesnt allow me to use it. I just checked its not there on prod servers. We have thousands of prod server so practically impossible to get the approval and then get it installed.
Use --argjson VariaX true to have the variable as boolean instead of string
Your production environment is broken if it requires using JSON without the tools to do so properly.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.