简体   繁体   English

Bash脚本以json格式打印输出

[英]Bash script print output in json format

I'm trying to curl a webpage and does some processing to it and in final i am trying to print in json format.(which actually needs to be in mongodb input) 我试图卷曲网页并对其进行一些处理,最后我试图以json格式打印(实际上需要在mongodb输入中)

so the input (which is read though curl) is 所以输入(通过curl读取)是

Input: 输入:

brendan google engineer
stones microsoft chief_engineer
david facebook tester

for the kind of processing, i'm assigning values to the variables ($name, $emloyer, $designation) 对于处理类型,我正在为变量分配值($ name,$ emloyer,$ designation)

my final command which converts to json is, 我最后转换为json的命令是

echo [{\"Name\":\"$name\"},{\"Employer\":\"$employer\"},{\"dDesignation\":\"$designation\"}]

The current output is, 当前输出是

[{"Name":"brendan","Employer":"google","Designation":"engineer"}]
[{"Name":"stones","Employer":"microsoft","Designation":"chief_engineer"}]
[{"Name":"david","Employer":"facebook","Designation":"tester"}]

but, i want the output in the same line separated by comma and square brackets in the start and end (not on every lines) 但是,我希望在同一行中的输出在开始和结尾处用逗号和方括号分隔(不是在每行上)

Expected output: 预期产量:

  [{"Name":"brendan","Employer":"google","Designation":"engineer"},{"Name":"stones","Employer":"microsoft","Designation":"chief_engineer"},
    {"Name":"david","Employer":"facebook","Designation":"tester"}]

any suggestions. 有什么建议么。

Conventional text-processing tools can't do this right for the general case. 传统的文本处理工具在一般情况下无法做到这一点。 There are a bunch of corner cases to JSON -- nonprintable and high-Unicode characters (and quotes) need to be escaped, for instance. JSON有很多特殊情况-例如,需要转义不可打印和高Unicode字符(和引号)。 Use a tool that's actually built for the job, such as jq : 使用为工作实际构建的工具,例如jq


jq -n -R '
[
  inputs |
  split(" ") |
  { "Name": .[0], "Employer": .[1], "Designation": .[2] }
]' <<EOF
brendan google engineer
stones microsoft chief_engineer
david facebook tester
EOF

...emits as output: ...作为输出发出:

[
  {
    "Name": "brendan",
    "Employer": "google",
    "Designation": "engineer"
  },
  {
    "Name": "stones",
    "Employer": "microsoft",
    "Designation": "chief_engineer"
  },
  {
    "Name": "david",
    "Employer": "facebook",
    "Designation": "tester"
  }
]

Something like this? 像这样吗

sep='['
curl "...whatever..." |
while read -r name employer designation; do
    printf '%s{"Name": "%s", "Employer": "%s", "Designation": "%s"}' "$sep" "$name" "$employer" "$designation"
    sep=', '
done
printf ']\n'

I do agree that this is brittle and error-prone; 我同意这是易碎且容易出错的; if you can use a JSON-aware tool like jq , by all means do that instead. 如果可以使用jq类的支持JSON的工具,则一定要这样做。

If you have access to jq 1.5 or later, then you can use inputs and may wish to consider using splits(" +") in case the tokens might be separated by more than one space: 如果可以访问jq 1.5或更高版本,则可以使用inputs并且可能希望考虑使用splits(" +") ,以防令牌可能被多个空格分隔:

jq -n -R '
  [inputs
   | [splits(" +")]
   | { "Name": .[0], "Employer": .[1], "Designation": .[2] }]'

If you do not have ready access to jq 1.5 or later, then please note that the following will work with jq 1.4: 如果您还没有访问jq 1.5或更高版本的权限,请注意以下内容适用于jq 1.4:

jq -R -s '
  [split("\n")[]
   | select(length>0)
   | split(" ")
   | { "Name": .[0], "Employer": .[1], "Designation": .[2] }]'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM