For loop on json array

(LuisN) #1

Tried to use an example alias for chatops but no success

What is the best way to parse each service(s) the name and its dependency in a nice format? Below is dummy data and heres the example i tried

{
  "result": {
    "raw": {
      "Device": "test1",
      "Count": "3",
      "results": [
        {
          "Services": {
            "Name": "Nginx",
            "Dependency": {
              "Name": "Mysql"
            }
          }
        },
        {
          "Services": {
            "Name": "Apache",
            "Dependency": {
              "Name": "Mysql"
            }
          }
        }
      ]
    }
  }
}

So far I have this but cant extract each services

result:
  format: |
        Details are as follows:
        Device: *{{execution.result.stdout.result.raw.Device}}*
#2

This format should return a list of service names:

# ...
result:
  format: |
    Details are as follows:
    {% for service in execution.result.raw.results -%}
    Device: *{{service.Name}}*
    {%+ endfor %}
(LuisN) #3

I Actually tried something like that. Let me paste the actual output

    "data": {
          "result": "{\"failed\": false, \"stderr\": \"\", \"return_code\": 0, \"succeeded\": true, \"stdout\": {\"result\": {\"raw\": {\"Device\": \"test1\", \"Count\": \"3\", \"results\": [{\"Services\": {\"Dependency\": {\"Name\": \"Mysql\"}, \"Name\": \"Nginx\"}}, {\"Services\": {\"Dependency\": {\"Name\": \"Mysql\"}, \"Name\": \"Apache\"}}]}}}}",

and i tried this

      format: |
            Details are as follows:
            Device: *{{execution.result.stdout.result.raw.Device}}*

            {% for service in execution.result.stdout.result.raw.results.Services -%}
            Service(s): *{{service.Services.Name}}* {{service.Services.Dependency.Name}}
            {%+ endfor %}

I get only

    Details are as follows:
    Device: *test1*
(LuisN) #4

Trying this and got further

    {% for service in execution.result.stdout.result.raw.results -%}
    Service(s): *{{service.Name}}* {{service.Services}}
    {%+ endfor %}

Service(s): ** {u'Dependency': {u'Name': u'Mysql'}, u'Name': u'Nginx'}
Service(s): ** {u'Dependency': {u'Name': u'Mysql'}, u'Name': u'Apache'}
#5

You’ll have to keep playing with it to get it to work.

Make sure you have a good understanding of exactly what your data (execution result) looks like.