Decoding JSON file using python

MRAB python at mrabarnett.plus.com
Wed May 27 19:08:42 EDT 2015


On 2015-05-27 23:23, Karthik Sharma wrote:
> I have the JSON structure shown below and the python code shown below to manipulate the JSON structure.
>
>      import json
>
>      json_input = {
>          "msgType": "0",
>          "tid": "1",
>          "data": "[{\"Severity\":\"warn\",\"Subject\":\"Reporting \",\"Message\":\"tdetails:{\\\"Product\\\":\\\"Gecko\\\",\\\"CPUs\\\":8,\\\"Language\\\":\\\"en-GB\\\",\\\"isEvent\\\":\\\">
>          "Timestamp": "1432703193431",
>          "Host": "myserver.com",
>          "Agent": "Experience",
>          "AppName": "undefined",
>          "AppInstance": "my_server",
>          "Group": "UndefinedGroup"
>      }
>
>
>      data = json_input['data']
>      tdetails = data['Message']
>      print('json_input {} \n\ndata {} \n\n tdetails {}\n\n'.format(json_input,data,tdetails))
>
> I am getting the error.
>
>      Traceback (most recent call last):
>        File "test_json.py", line 19, in <module>
>          tdetails = data['Message']
>      TypeError: string indices must be integers, not str
>
> The JSON structure is valid as shown by http://jsonlint.com/
>     I want to be able to access the different fields inside `data` such as `severity`, `subject` and also fields inside `tdetails` such as `CPUs` and `Product`. How do I do this?
>
json_input['Message'] is a string, not a dict.



More information about the Python-list mailing list