【问题标题】:Encode JSON in bash script在 bash 脚本中编码 JSON
【发布时间】:2020-02-18 10:20:45
【问题描述】:

我正在尝试使用 Zabbix API 将模板导入我的 Zabbix 服务器,我使用了这个:

fileJSON=\""$(cat template_file)"\"

curl  -s -X POST -H 'Content-Type: application/json-rpc' -d '{
    "jsonrpc": "2.0",
    "method": "configuration.import",
    "params": {
        "format": "json",
        "rules": {
            "templates": {
                "createMissing": true,
                "updateExisting": true
            }
        },
        "source": $fileJSON
    },
    "auth": "6a977cd94b26b6156698459ac4d0f769",
    "id": 1
}' 'http://127.0.0.1/zabbix/api_jsonrpc.php' | jq '.'

这是输出:

{"jsonrpc":"2.0","error":{"code":-32700,"message":"Parse error","data":"Invalid JSON. An error occurred on the server while parsing the JSON text."},"id":null}

我只是没有看到任何错误……我尝试使用在线解析器网站,但他没有看到任何错误。 这个var直接填充了Zabbix的导出方式(JSON格式)。 这是文件的内容:

{
  "jsonrpc": "2.0",
  "result": "{\"zabbix_export\":{\"version\":\"4.4\",\"date\":\"2020-02-18T08:47:31Z\",\"groups\":[{\"name\":\"System\"}],\"templates\":[{\"template\":\"Template Systeme\",\"name\":\"Template Systeme\",\"groups\":[{\"name\":\"System\"}],\"discovery_rules\":[{\"name\":\"D\\u00e9couverte des services\",\"key\":\"service.discovery\",\"delay\":\"30s\"}]}]}}",
  "id": 1
}

我认为错误是fileJSON的格式,如何将这个变量编码为JSON格式?

【问题讨论】:

  • 作为一个建议,您为什么不在将 JSON 放入 Curl 之前创建它,以便检查其有效性?

标签: json bash zabbix


【解决方案1】:

您使用单引号 '{...}' 提交您的 JSON,这不会导致您的 $fileJSON 插值 - 因此您会收到该错误。

改用双引号:

curl  -s -X POST -H 'Content-Type: application/json-rpc' -d "{
...

您还需要引用 JSON 的内引号。

还有一种更简单的方法来插入$fileJSON,像这样使用它:

curl  -s -X POST -H 'Content-Type: application/json-rpc' -d '{
    "jsonrpc": "2.0",
    "method": "configuration.import",
    "params": {
        "format": "json",
        "rules": {
            "templates": {
                "createMissing": true,
                "updateExisting": true
            }
        },
        "source": '"$fileJSON"'
    },
    "auth": "6a977cd94b26b6156698459ac4d0f769",
    "id": 1
}' ...

这样可以省去引用内引号的麻烦

【讨论】:

  • 不,单引号是使用 cURL 发送 JSON 数据的正确方法。看到这个 SO post
  • shell 插值发生在参数传递给命令之前,并且单引号会阻止它。 curl 根本不知道 $fileJSON 需要插值
  • 对。 -d 的参数周围的单引号表示传递给 curl 的数据包含文字 9 字符序列 $fileJSON;没有你加载到 shell 变量 named $fileJSON. 的文件的内容
  • 感谢您的回答,但不要解决问题...
  • @BlueStraax 在这种情况下返回的错误是什么?我怀疑$fileJSON 的插值并不顺利。你介意分享那个变量的内容吗?
【解决方案2】:

来自API documentation

source (required) - string - 包含 配置数据。

您可以找到一个 XML 源示例:

"source": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><zabbix_export><version> [cut]

您以source 发送configuration.export 调用的完整输出:

{
  "jsonrpc": "2.0",
  "result": "{\"zabbix_export\":{\"version\":\"4.4\",\"date\":\"2020-02-18T08:47:31Z\",\"groups\":[{\"name\":\"System\"}],\"templates\":[{\"template\":\"Template Systeme\",\"name\":\"Template Systeme\",\"groups\":[{\"name\":\"System\"}],\"discovery_rules\":[{\"name\":\"D\\u00e9couverte des services\",\"key\":\"service.discovery\",\"delay\":\"30s\"}]}]}}",
  "id": 1
}

虽然您只需发送序列化字符串,它应该是您的fileJSON"result" 字段的值:

"source": "{\"zabbix_export\":{\"  [cut]

【讨论】:

  • 谢谢你的回答,我认为你是对的,但没有解决问题……无论有没有反斜杠,总是同样的错误……知道吗?
  • 尝试不使用外部文件,在 curl 数据中使用静态源。但更好的办法是避免手动“卷曲”:使用处理转义、身份验证等的 API 实现......我使用 pyzabbix,但这是个人品味决定。
  • 总是同样的错误我想我会使用 xml 格式或 pyzabbix。谢谢
【解决方案3】:

template_file 输出看,result 键不是 JSON,而是 JSON 字符串。你试过不JSON dumping吗?

【讨论】:

  • 我没有转储它,默认情况下就是这样。我真的不知道 JSON 和 JSON 字符串有什么区别,这就是为什么我的要求是如何将其转换为 JSON 格式
猜你喜欢
  • 2012-08-30
  • 1970-01-01
  • 2021-09-01
  • 2013-09-29
  • 2012-08-06
  • 2023-03-07
  • 2014-12-08
  • 2018-07-24
相关资源
最近更新 更多