Skip to content

Get values as a list of dictionaries from a JSON object in Python

Advertisements
{
	"pb-IF4JVRcuLA==": {
		"name_html": "Soђสή",
		"mkc": 1,
		"kills": 14,
		"deaths": 4,
		"lastCharacter": "Spaz",
		"lastName": "Soђสή",
		"games": 3,
		"scores": 439,
		"aid": "pb - IF4JVRcuLA == "
	},
	"pb-IF4rVWseCg==": {
		"name_html": "Android48457626",
		"mkc": 0,
		"kills": 0,
		"deaths": 6,
		"lastCharacter": "Kronk",
		"lastName": "peo",
		"games": 3,
		"scores": 205,
		"aid": "pb - IF4rVWseCg == "
	}
}

Let’s take the JSON above as an example. I need the values as a list of dictionaries in Python.

import json

json_object = json.loads('{"pb-IF4JVRcuLA==":{"name_html":"s","mkc":1,"kills":14,"deaths":4,"lastCharacter":"Spaz","lastName":"s","games":3,"scores":439,"aid":"pb - IF4JVRcuLA == "},"pb-IF4rVWseCg==":{"name_html":"Android48457626","mkc":0,"kills":0,"deaths":6,"lastCharacter":"Kronk","lastName":"peo","games":3,"scores":205,"aid":"pb - IF4rVWseCg == "}}')
pairs = json_object.items()

res = []
for key, value in pairs:
    res.append(value)

print(res)

#[{u'name_html': u's', u'mkc': 1, u'kills': 14, u'deaths': 4, u'lastCharacter': u'Spaz', u'lastName': u's', u'games': 3, u'scores': 439, u'aid': u'pb - IF4JVRcuLA == '}, {u'name_html': u'Android48457626', u'mkc': 0, u'kills': 0, u'deaths': 6, u'lastCharacter': u'Kronk', u'lastName': u'peo', u'games': 3, u'scores': 205, u'aid': u'pb - IF4rVWseCg == '}]

This is how I did it.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.