【问题标题】:How to compare Json data with array using Shell Script?如何使用 Shell 脚本将 Json 数据与数组进行比较?
【发布时间】:2018-07-05 04:22:57
【问题描述】:

我有一个 json 例如

var bbview ={  
   "tbl_am_api":[  
      {  
         "Modified_User":"user1",
         "Modified_Time":"04-Jul-2018 01:40:05",
         "Line_Number":"SS001",
         "Service_Type":"BB3",
         "Status":"Yes",
         "ID":3144526000014337832,
         "Added_Time":"04-May-2018 11:37:29"
      },
      {  
         "Modified_User":"user2",
         "Modified_Time":"04-Jul-2018 01:40:05",
         "Line_Number":"SS002",
         "Service_Type":"BB2",
         "Status":"Yes",
         "ID":3144526000014337832,
         "Added_Time":"04-May-2018 11:37:29"
      },
      {  
         "Modified_User":"user3",
         "Modified_Time":"04-Jul-2018 01:40:05",
         "Line_Number":"SS004",
         "Service_Type":"BB1",
         "Status":"No",
         "ID":3144526000014337832,
         "Added_Time":"04-May-2018 11:37:29"
      }
   ]
};

我想比较这个 json 数据和数组。主键作为行号。

arrayA = {[{Line_Number : SS001, Service_Type : BB3; Status : Yes}]}

arrayA 有 Line_Number SS001。在 json 中找到这个 Line_Number 并比较 Service_Type 值和 Status 值是否相同。我想在 bash 文件中使用 Shell 脚本编写。我不精通shell脚本。请帮帮我。

更新: 我尝试使用以下 bash 代码。但还是失败了。请给我建议

echo "Download FMS AM API File"
rm -rf tbl_am_api_Report?authtoken=da84d49f334c33b88d30dd2c947a4ff0 && wget -q https://creator.zoho.com/api/json/fixed-management-system/view/tbl_am_api_Report?authtoken=da84d49f334c33b88d30dd2c947a4ff0&scope=creatorapi&zc_ownername=tmlbroadband < /dev/null

cat > tbl_api_Report?authtoken=da84d49f334c33b88d30dd2c947a4ff0 //read json file
for row in $(echo "${apiview}" | jq -r '.[] | @base64'); do
    _jq() {
     echo ${row} | base64 --decode | jq -r ${1}
    }

   echo $(_jq '.name') >> info.txt
done

mail -s "Test email" aa@gmail.com -A info.txt < /dev/null

【问题讨论】:

  • 1.请遵循minimal reproducible example 准则。 (预期的输出是什么?) 2. “更新”下的代码充其量似乎是完全不相关的,最坏的情况是胡言乱语。我建议删除它,否则您的 Q 可能会被否决。

标签: arrays json bash jq string-comparison


【解决方案1】:

arrayA 的值不是 JSON,所以我将让您弄清楚如何从 arrayA 中提取 Line_Number 和 Status 的值(但见下文)。一旦这些值可用,就可以按照此处的说明进行操作:

#!/bin/bash

bbview='...' # as above

echo "$bbview" |
  jq --arg Line_Number SS001 --arg Status Yes '
    .tbl_am_api
    | map(select(.Line_Number==$Line_Number and .Status==$Status)) '

输出

[
  {
    "Modified_User": "user1",
    "Modified_Time": "04-Jul-2018 01:40:05",
    "Line_Number": "SS001",
    "Service_Type": "BB3",
    "Status": "Yes",
    "ID": 3144526000014338000,
    "Added_Time": "04-May-2018 11:37:29"
  }
]

真/假

根据对问题的不同解读,以下变体可能是相关的:

echo "$bbview" |
  jq --arg Line_Number SS001 --arg Status Yes '
    .tbl_am_api
    | map(select(.Line_Number==$Line_Number) | .Status==$Status) '

数组A

如果您使用支持关联数组的 ba​​sh 版本,您可以将 arrayA 定义为关联数组,如下所示:

declare -A arrayA
arrayA=([Line_Number]=SS001 [Service_Type]=BB3 [Status]=Yes)

然后要检索与 Line_Number 关联的值,您可以编写:${arrayA[Line_Number]};等等

【讨论】:

    猜你喜欢
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    • 1970-01-01
    • 2021-10-12
    相关资源
    最近更新 更多