except UserError
except UserError

Reputation: 191

Python Convert json.loads() to Dictionary

I have something like:

sysbus = json.loads(file)

print sysbus
>>> [{u'eth3': u'pci@0000:03:00.1', u'eth2': u'pci@0000:03:00.0', u'eth1': u'pci@0000:06:00.1', u'eth0': u'pci@0000:06:00.0'}]

I would like to convert sysbus into a dictionary.

{'eth3': 'pci@0000:03:00.1', 'eth2': 'pci@0000:03:00.0', 'eth1': 'pci@0000:06:00.1', 'eth0': 'pci@0000:06:00.0'}

What is the cleanest way to convert this?

Upvotes: 0

Views: 1902

Answers (1)

Thomas Orozco
Thomas Orozco

Reputation: 55197

It looks like you just want the first item from this list, so get that:

sysbus = json.loads(file)[0]

Do you expect this file could somehow contain multiple entries in the list? If yes, then you might want to check the length for the list first.

Upvotes: 2

Related Questions