0

Hi folks im new to asking questions in stack but I want to know what is the process in order to remove the 'name' value from the json output below i know how to get the individual names through indexingname = [groups['groups'][0]['name'] for groups in result] but how do i get both names

from pyzabbix import ZabbixAPI

import json

z = ZabbixAPI(url='https://zabbix.server,user='xxxx',password='xxxx')
result = z.host.get(output='groupid',selectGroups='extend', filter={'host' : 'mailpphfprd01'})


name = [groups['groups'] for groups in result]

name 

Json Output:

[[{u'flags': u'0',
   u'groupid': u'110',
   u'internal': u'0',
   u'name': u'- GSOC - Systems'},
  {u'flags': u'0',
   u'groupid': u'313',
   u'internal': u'0',
   u'name': u'Applications - Email Digest'}]]

Required output

[u'- GSOC - Systems' u'Applications - Email Digest']

1 Answer 1

1

You should be able to do it with a list-comprehension:

json_output = [
  [
    {
      "internal": "0",
      "flags": "0",
      "groupid": "110",
      "name": "- GSOC - Systems"
    },
    {
      "internal": "0",
      "flags": "0",
      "groupid": "313",
      "name": "Applications - Email Digest"
    }
  ]
]

names = [d['name'] for d in json_output[0]]

print(names)  # -> [u'- GSOC - Systems', u'Applications - Email Digest']
Sign up to request clarification or add additional context in comments.

2 Comments

json_output[0] why is the index placed here is it to represent the internal []
The json output is a list of a list of dictionaries, so I just hardcoded the index of the first entry in the outer list — in other words, json_output[0] is the first (and only) entry of that outermost list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.