[triangle-zpug] python parsing JSON

David Handy david at handysoftware.com
Thu Dec 25 04:04:55 UTC 2014


Hi Justin -
 
While my children are nestled all snug in their beds, with visions of sugarplums dancing in their heads, I'll be happy to answer your JSON question!

The JSON data you supplied is not formed correctly. It looks like it is supposed to be a JSON object, which is roughly equivalent to a Python dictionary, but it is missing the starting and ending curly braces. Also there is an extra comma after the second dictionary item, which will cause Python's json parser to raise an exception.

Here is some working Python code that includes a fixed version of your JSON data, and the code to parse it and extract macaddress:

##### begin code

from __future__ import print_function
import json

json_data_str = """
{
 "log": {
   "id": "http://192.168.43.81:8080/api/collections/nodes/node1/log",
   "name": "log"
 },
 "tags": [],
 "facts": {
   "hardwareisa": "x86_64",
   "macaddress": "08:00:27:e7:06:4d",
   "architecture": "x86_64",
   "hardwaremodel": "x86_64",
   "processor0": "Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz",
   "processorcount": "1",
   "interfaces": "enp0s17,lo",
   "ipaddress_enp0s17": "192.168.1.63"
 }
}
"""

data = json.loads(json_data_str)

print(data["facts"]["macaddress"])

##### end code

I put the above code into a file named get_macaddress.py and ran it. Here is the result:

$ python get_macaddress.py 
08:00:27:e7:06:4d

Again, please note in my code example above, I added beginning "{" and "}" characters to your JSON data. And I put the JSON data inside a triple-quoted Python string. I don't know how applicable that is to your situation because I don't know from where you are getting your data, but I hope this helps.

David H

Update: I saw Chris Church's email right when I was about to hit the "Send" button on this. On well, now you've got two code samples to look at!
 
-----Original Message-----
From: "Justin Parker" <xjparkerx at gmail.com>
Sent: Wednesday, December 24, 2014 10:38pm
To: "TriPython" <triangle-zpug at starship.python.net>
Subject: [triangle-zpug] python parsing JSON



I have no doubt that this has probably been answered in some form or 
fashion multiple times. Unfortunately, I am having a terrible time 
getting something to work. Any help would be greatly appreciated.

I have this json data:

"log": {
 "id": "http://192.168.43.81:8080/api/collections/nodes/node1/log",
 "name": "log"
 },
 "tags": [],
 "facts": {
 "hardwareisa": "x86_64",
 "macaddress": "08:00:27:e7:06:4d",
 "architecture": "x86_64",
 "hardwaremodel": "x86_64",
 "processor0": "Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz",
 "processorcount": "1",
 "interfaces": "enp0s17,lo",
 "ipaddress_enp0s17": "192.168.1.63"
 },

I'm trying to parse this data to get the value for macaddress using the 
standard json library.

Again any help would be greatly appreciated.
Justin

_______________________________________________
triangle-zpug mailing list
triangle-zpug at starship.python.net
http://starship.python.net/mailman/listinfo/triangle-zpug




More information about the triangle-zpug mailing list