2

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.

4 Answers 4

1

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']]
Sign up to request clarification or add additional context in comments.

1 Comment

This is it! Thank you very much for your valuable help.
0
{
    "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"]

2 Comments

This is exactly what I thought. But this is a live json data feed from another source. I need to parse this realtime. How can I remove this in my code after jaysonData = json.load(urllib2.urlopen('URL'))
If you can't change response then do again res = json.load(jaysonData["Tag1"])
0

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"
    }
}

4 Comments

print jaysonData["Tag1"]["TagX"][0]["TagB"] TypeError: string indices must be integers
@user611811 remove the double quotations also as pointed out by Naresh.
How can I strip the double quotations with python. This json file is not local and I fetch it every minute. Can you show me at least the way?
@user611811 Can you give the link of the API or from where you are collecting this data ?
0

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"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.