温馨提示:本文翻译自stackoverflow.com,查看原文请点击:python - ansible convert escaped json string to json
ansible jmespath json python

python - ansible将转义的json字符串转换为json

发布于 2020-09-04 00:35:43

我有一个看起来像这样的Ansible输出,但一直努力将其解析为JSON而没有转义的字符串。

{
    "msg": [
        "{",
        "\t\"inprog\" : [",
        "\t\t{",
        "\t\t\t\"desc\" : \"conn174272\",",
        "\t\t\t\"threadId\" : \"139899377280768\",",
        "\t\t\t\"connectionId\" : 174272,",
        "\t\t\t\"client\" : \"127.0.0.1:43365\",",
        "\t\t\t\"active\" : true,",
        "\t\t\t\"opid\" : 35571992,",
        "\t\t\t\"secs_running\" : 0,",
        "\t\t\t\"microsecs_running\" : NumberLong(16),",
        "\t\t\t\"op\" : \"command\",",
        "\t\t\t\"ns\" : \"admin.$cmd\",",
        "\t\t\t\"query\" : {",
        "\t\t\t\t\"currentOp\" : 1",
        "\t\t\t},",
        "\t\t\t\"numYields\" : 0,",
        "\t\t\t\"locks\" : {",
        "\t\t\t\t",
        "\t\t\t},",
        "\t\t\t\"waitingForLock\" : false,",
        "\t\t\t\"lockStats\" : {",
        "\t\t\t\t",
        "\t\t\t}",
        "\t\t}",
        "\t],",
        "\t\"ok\" : 1",
        "}"
    ]
}

理想情况下,输出应类似于以下内容:

{
    "inprog": [
        {
            "desc": "conn5404",
            "threadId": "139922277680896",
            "connectionId": 5404,
            "client": "127.0.0.1:50726",
            "active": true,
            "opid": 225819,
            "secs_running": 0,
            "microsecs_running": NumberLong(20),
            "op": "command",
            "ns": "admin.$cmd",
            "query": {
                "currentOp": 1
            },
            "numYields": 0,
            "locks": {
            },
            "waitingForLock": false,
            "lockStats": {
            }
        }]
}

我已经尝试过JMESPath和Ansible过滤器,但是没有运气。任何提示都会有所帮助。最近尝试是

- name: get ops
  shell:
    cmd: mongo --eval  "db.currentOp()"
  register: currentOp

- debug:
    msg: "{{ currentOp.stdout_lines[2:] | to_json }}"

结果是:

{
    "msg": "[\"{\", \"\\t\\\"inprog\\\" : [\", \"\\t\\t{\", \"\\t\\t\\t\\\"desc\\\" : \\\"conn174318\\\",\", \"\\t\\t\\t\\\"threadId\\\" : \\\"139899381491456\\\",\", \"\\t\\t\\t\\\"connectionId\\\" : 174318,\", \"\\t\\t\\t\\\"client\\\" : \\\"127.0.0.1:43374\\\",\", \"\\t\\t\\t\\\"active\\\" : true,\", \"\\t\\t\\t\\\"opid\\\" : 35579590,\", \"\\t\\t\\t\\\"secs_running\\\" : 0,\", \"\\t\\t\\t\\\"microsecs_running\\\" : NumberLong(15),\", \"\\t\\t\\t\\\"op\\\" : \\\"command\\\",\", \"\\t\\t\\t\\\"ns\\\" : \\\"admin.$cmd\\\",\", \"\\t\\t\\t\\\"query\\\" : {\", \"\\t\\t\\t\\t\\\"currentOp\\\" : 1\", \"\\t\\t\\t},\", \"\\t\\t\\t\\\"numYields\\\" : 0,\", \"\\t\\t\\t\\\"locks\\\" : {\", \"\\t\\t\\t\\t\", \"\\t\\t\\t},\", \"\\t\\t\\t\\\"waitingForLock\\\" : false,\", \"\\t\\t\\t\\\"lockStats\\\" : {\", \"\\t\\t\\t\\t\", \"\\t\\t\\t}\", \"\\t\\t}\", \"\\t],\", \"\\t\\\"ok\\\" : 1\", \"}\"]"
}

这个想法是从输出创建一个新的json数组。例如,[{"secs_running": 123, "opid": 1232342}]

查看更多

提问者
emmanueledu
被浏览
1
emmanueledu 2020-05-15 07:42

mongo --quiet --eval "print(JSON.stringify(db.currentOp()))"由于Mongo shell是一个Javascript交互式shell JSON.stringify,因此神奇地返回了JSON响应,从而解决了该问题