Decoding JSON file using python
Denis McMahon
denismfmcmahon at gmail.com
Thu May 28 14:49:35 EDT 2015
On Thu, 28 May 2015 13:32:39 +1000, Cameron Simpson wrote:
> On 28May2015 01:38, Jon Ribbens <jon+usenet at unequivocal.co.uk> wrote:
>>On 2015-05-27, Karthik Sharma <karthik.sharma at gmail.com> wrote:
>>> I tried modifying the program as follows as per your
>>> suggestion.Doesn't seem to work.
>>
>>That's because you didn't modify the program as per their suggestion,
>>you made completely different changes that bore no relation to what they
>>said.
>
> Actually, his changes looked good to me. He does print from data first,
> but only for debugging. Then he goes:
>
> message = json.loads(data)
>
> and tried to access message['Message'].
>
> However I am having trouble reproducing his issue because his quoted
> code is incorrect. I've tried to fix it, as listed below, but I don't
> know what is really meant to be in the 'data" string.
it looks like data is a broken array of one object, part of which is a
further quoted json string.
There should be a string value after the isEvent but I have no idea what
it should be, nor what else should come after.
"message":"tdetails":{att:val pairs}
is also wrong in the first level of inner json.
I think he wants data[0]['message'], but the inner json strings are
broken too, and should look more like.
data: "[{\"Severity\":\"warn\",\"Subject\":\"Reporting\",\"Message\":
\"tdetails\",\"attr_name\":\"{\\\"Product\\\":\\\"Gecko\\\",\\\"CPUs\\
\":8,\\\"Language\\\":\\\"en-GB\\\",\\\"isEvent\\\":\\\"attr_value\\\"}\"}
]",
In this model, if the data attribute is a json string, then
data maps to a list / array
data[0] maps to an object / dictionary
data[0]["Message"] maps to the string literal "tdetails"
data[0]["attr_name"] maps to a string representation of a json ob with
another level of escaping.
That string can then be loaded, eg:
attr_name = json.loads(data[0]["attr_name"])
See: http:/www.sined.co.uk/python/nested_json.py.txt
--
Denis McMahon, denismfmcmahon at gmail.com
More information about the Python-list
mailing list