user611811
user611811

Reputation: 45

Parsing JSON with Python - How to access deep elements

Hi I am really new to JSON and Python, here is my dilemma, it's been bugging me for two days. Here is the sample json that I want to parse.

{
    "Tag1":"{
"TagX": [
  {
    "TagA": "A",
    "TagB": 1.6,
    "TagC": 1.4,
    "TagD": 3.5,
    "TagE": "01",
    "TagF": null
  },
  {
    "TagA": "A",
    "TagB": 1.6,
    "TagC": 1.4,
    "TagD": 3.5,
    "TagE": "02",
    "TagF": null
  }
],  
"date": "10.03.2017 21:00:00"
    }"
}

Here is my python code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import json
import urllib2

jaysonData = json.load(urllib2.urlopen('URL'))
print jaysonData["Tag1"]

How can I get values of TagB and TagC? When I try to access them with

jaysonData = json.load(urllib2.urlopen('URL'))
print jaysonData["Tag1"]["TagX"]["TagB"]

The output is:

TypeError: string indices must be integers

When I do this:

print jaysonData["Tag1"]

The output is:

{
"TagX": [
  {
    "TagA": "A",
    "TagB": 1.6,
    "TagC": 1.4,
    "TagD": 3.5,
    "TagE": "01",
    "TagF": null
  },
  {
    "TagA": "A",
    "TagB": 1.6,
    "TagC": 1.4,
    "TagD": 3.5,
    "TagE": "02",
    "TagF": null
  }
],  
"date": "10.03.2017 21:00:00"
    }"

I need to reach TagX, TagD, TagE but the below code gives this error:

print jaysonData["Tag1"]["TagX"]

prints

    print jaysonData["Tag1"]["TagX"]
TypeError: string indices must be integers

How can I access TagA to TagF with python? Thanks in advance.

Upvotes: 2

Views: 930

Answers (4)

Ben
Ben

Reputation: 6777

In the returned JSON, the value of Tag1 is a string, not more JSON. It does appear to be JSON encoded as a string though, so convert that to json once more:

jaysonData = json.load(urllib2.urlopen('URL'))
tag1JaysonData = json.load(jaysonData['Tag1'])
print tag1JaysonData["TagX"]

Also note that TagX is a list, not a dictionary, so there are multiple TagBs in it:

print [x['TagB'] for x in tag1JaysonData['TagX']]

Upvotes: 1

SHAHS
SHAHS

Reputation: 472

You need to consider what values your about to access e.g a dict or list ? You always access dict element by key and list element by index.

to remove the quotes you can do after reading data from url.

import ast
JsonData = ast.literal_eval(jaysonData["Tag1"])
JsonData["Tagx"][0]["TagB"]

Upvotes: 0

MD. Khairul Basar
MD. Khairul Basar

Reputation: 5110

TagX is a list consisting of 2 dictionaries and TagB is a dictionary.

print jaysonData["Tag1"]["TagX"][0]["TagB"]

You need to remove double quotations before and after the curly braces of Tag1.

{
    "Tag1":{
"TagX": [
  {
    "TagA": "A",
    "TagB": 1.6,
    "TagC": 1.4,
    "TagD": 3.5,
    "TagE": "01",
    "TagF": null
  },
  {
    "TagA": "A",
    "TagB": 1.6,
    "TagC": 1.4,
    "TagD": 3.5,
    "TagE": "02",
    "TagF": null
  }
],  
"date": "10.03.2017 21:00:00"
    }
}

Upvotes: 0

Naresh Chaudhary
Naresh Chaudhary

Reputation: 735

{
    "Tag1":{
"TagX": [
  {
    "TagA": "A",
    "TagB": 1.6,
    "TagC": 1.4,
    "TagD": 3.5,
    "TagE": "01",
    "TagF": null
  },
  {
    "TagA": "A",
    "TagB": 1.6,
    "TagC": 1.4,
    "TagD": 3.5,
    "TagE": "02",
    "TagF": null
  }
],  
"date": "10.03.2017 21:00:00"
    }
}

You have to remove double qoutation before curly bracket of Tag1. I have removed in above sample.

and acess like this.

print jaysonData["Tag1"]["TagX"][0]["TagA"]

Upvotes: 0

Related Questions