Warm tip: This article is reproduced from stackoverflow.com, please click
ansible jmespath json python

ansible convert escaped json string to json

发布于 2020-08-03 05:12:00

I have an Ansible output that looks like this but have struggled to parse this to JSON without the escaped strings.

{
    "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",
        "}"
    ]
}

ideally the output should resemble something like:

{
    "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": {
            }
        }]
}

I've tried JMESPath and Ansible filters but no luck. any tips will be helpful. Latest attempt was

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

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

which resulted in:

{
    "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\", \"}\"]"
}

the idea is to create a new array of json from the output. for example, [{"secs_running": 123, "opid": 1232342}]

Questioner
emmanueledu
Viewed
21
emmanueledu 2020-05-15 07:42

resolved it with mongo --quiet --eval "print(JSON.stringify(db.currentOp()))" since Mongo shell is a Javascript interactive shell JSON.stringify did the magic to return JSON response