Content shell output json format

For output shell script, if you want to output the contents of json format, we can use python -m json.tool command
such as
echo '{ "name": " zhangsan", "age": "18"}' | python -m json .tool

 
 

First, there must be outside the braces single quotes '', as in the shell without braces {} is equivalent to the single quotes an anonymous function
without single quotation marks is equivalent to the output of echo { "name": "zhangsan" , "age": "18" } anonymous function return value.
Secondly, braces inside the string, you must use "" double quotation marks, can not use the '' single quotation marks, otherwise it will error.

When the braces with variable output this how it?
echo back the contents have been '' single quotation marks up, shell itself has been impossible to go to resolve the variable, then we can only rank manually to resolve, this time to think eval command, a function eval is to your own parameters variables are expanded, specifically with respect to the eval command, its own online search bar, direct answer below.
echo $ (eval echo '{ " name": "zhangsan", "age": "" $ age ""}') | python -m json.tool


 
 

Note that the first echo is not necessary, it can also be eval echo '{ "name": "zhangsan", "age": "" $ age ""}' | python -m json.tool. But inside the braces, "" double quotes must be backslash \ marked out, because eval performing variable expansion when the braces will be "" parsed out, when not a backslash \, whose output goes became the


 
 

For python -m json.tool, "" double quotation marks are necessary. So brace inside the "" double quotes must be backslash \ marked out.



Guess you like

Origin www.cnblogs.com/lgj8/p/12325488.html