1

i'm trying to parse my data from json. I have a function that loads all data... Data are like this:

devices_infos = get_devices()

    {
  "page" : 0,
  "size" : 20,
  "totalCount" : 2,
  "data" : [ {
    "namespace" : "android",
    "id" : "0000001111111",
    "creationTs" : 1516216756819,
    "name" : "Eric's phone",
    "description" : "This device was auto registered by [mqtt] ",
    "tags" : [ ],
    "properties" : { },
    "lastUpdateTs" : 1518610495832,
    "connected" : false,
    "path" : [ ],
    "metadata" : {
      "api_key_id" : "XXX000XXX000XXX",
      "connection_start_time" : "2018-02-14T12:14:04.778Z",
      "mqtt_version" : 4,
      "mqtt_username" : "json+device",
      "mqtt_timeout" : 20,
      "remote_addr" : "00.00.00.00/PORT"
    },
    "groupId" : "root",
    "groupPath" : "/"
  }, {
    "namespace" : "sensor",
    "id" : "temp001",
    "creationTs" : 1520415684605,
    "name" : "mySensor001",
    "description" : "moisture sensor",
    "tags" : [ "france", "lyon" ],
    "properties" : {
      "manufacturer" : "miel",
      "model" : "MoistureSensorV3"
    },
    "lastUpdateTs" : 1520415684605,
    "connected" : false,
    "path" : [ ],
    "groupId" : "root",
    "groupPath" : "/"
  } ]

I would like to extract the name of the devices via a for loop like this:

if devices_infos is not None:
    print('Devices are: ')
      for each in devices_infos['data'][0]['name']:
        print (each)
else:
    print("[!] Request failed")

This only print me the first device, ie Eric's phone , i would like to have too mySensor001.

3
  • ['data'][0] means the first element (index zero) Commented Mar 7, 2018 at 11:36
  • yeah i know, but if i put ['data']['name'] it doesn't work, it would be an integer not a string. Otherwise i see your solution below, it works for me. Commented Mar 7, 2018 at 12:35
  • 1
    ['data']['name'] does not work because it applies to the list itself not the list elements. The ['blabla'] selector is for object key/value pairs like {"a": 42} and does not apply to lists, for lists the [ ] operator is directly accessing an index (first element, second element, ...). Example by Pradam is a list comprehension which another way to access elements in python. Commented Mar 7, 2018 at 12:53

2 Answers 2

1

Remove the [0]

  for device in devices_infos['data']:
      name = device['name']

[...] means array in a JSON context, so you must iterate (loop) on it.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

>>> import json
>>> str = '''
...  {
...   "page" : 0,
...   "size" : 20,
...   "totalCount" : 2,
...   "data" : [ {
...     "namespace" : "android",
...     "id" : "0000001111111",
...     "creationTs" : 1516216756819,
...     "name" : "Eric's phone",
...     "description" : "This device was auto registered by [mqtt] ",
...     "tags" : [ ],
...     "properties" : { },
...     "lastUpdateTs" : 1518610495832,
...     "connected" : false,
...     "path" : [ ],
...     "metadata" : {
...       "api_key_id" : "XXX000XXX000XXX",
...       "connection_start_time" : "2018-02-14T12:14:04.778Z",
...       "mqtt_version" : 4,
...       "mqtt_username" : "json+device",
...       "mqtt_timeout" : 20,
...       "remote_addr" : "00.00.00.00/PORT"
...     },
...     "groupId" : "root",
...     "groupPath" : "/"
...   }, {
...     "namespace" : "sensor",
...     "id" : "temp001",
...     "creationTs" : 1520415684605,
...     "name" : "mySensor001",
...     "description" : "moisture sensor",
...     "tags" : [ "france", "lyon" ],
...     "properties" : {
...       "manufacturer" : "miel",
...       "model" : "MoistureSensorV3"
...     },
...     "lastUpdateTs" : 1520415684605,
...     "connected" : false,
...     "path" : [ ],
...     "groupId" : "root",
...     "groupPath" : "/"
...   } ]}'''
>>> d = json.loads(str)
>>> d
{u'totalCount': 2, u'data': [{u'lastUpdateTs': 1518610495832L, u'description': u'This device was auto registered by [mqtt] ', u'tags': [], u'namespace': u'android', u'creationTs': 1516216756819L, u'properties': {}, u'connected': False, u'groupPath': u'/', u'groupId': u'root', u'path': [], u'metadata': {u'api_key_id': u'XXX000XXX000XXX', u'remote_addr': u'00.00.00.00/PORT', u'mqtt_username': u'json+device', u'mqtt_timeout': 20, u'connection_start_time': u'2018-02-14T12:14:04.778Z', u'mqtt_version': 4}, u'id': u'0000001111111', u'name': u"Eric's phone"}, {u'lastUpdateTs': 1520415684605L, u'description': u'moisture sensor', u'tags': [u'france', u'lyon'], u'namespace': u'sensor', u'creationTs': 1520415684605L, u'properties': {u'model': u'MoistureSensorV3', u'manufacturer': u'miel'}, u'connected': False, u'groupPath': u'/', u'groupId': u'root', u'path': [], u'id': u'temp001', u'name': u'mySensor001'}], u'page': 0, u'size': 20}
>>> for i in d['data']:
...     print(i['name'])
...
Eric's phone
mySensor001

I hope this resolves your issue.

1 Comment

You are always welcome. Please do not forget to select the answer that resolved your issue. It will be helpful to all who reaches your question with same query.

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.