LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-14-2017, 06:10 PM   #1
slimcharles
LQ Newbie
 
Registered: Dec 2016
Distribution: Ubuntu 20.04 & Debian 11
Posts: 13

Rep: Reputation: Disabled
Parsing Json with Python


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.
Code:
{
	"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:
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
Code:
jaysonData = json.load(urllib2.urlopen('URL'))
print jaysonData["Tag1"]["TagX"]["TagB"]
The output is:
Code:
TypeError: string indices must be integers
 
Old 03-14-2017, 06:24 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,249

Rep: Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323
Quote:
Code:
print jaysonData["Tag1"]
And what does that print out?

Also, what does this print out?

Code:
print jaysonData["Tag1"]["TagX"]
 
Old 03-15-2017, 12:29 AM   #3
slimcharles
LQ Newbie
 
Registered: Dec 2016
Distribution: Ubuntu 20.04 & Debian 11
Posts: 13

Original Poster
Rep: Reputation: Disabled
Code:
print jaysonData["Tag1"]
prints

Code:
{
"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"
	}"

Code:
print jaysonData["Tag1"]["TagX"]
prints

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

Last edited by slimcharles; 03-15-2017 at 01:03 AM. Reason: typo
 
Old 03-15-2017, 11:30 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,691
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
There's a good synopsis from StackOverflow ...

Python supplies a module that you can access with, simply, import json. All the dirty-work of dealing with a JSON file has been done for you.

Because JSON, and XML, are fairly-well universal, every language that I know of provides one or more "canned" ways of dealing with these file formats.

Quote:
Actum Ne Agas: "Do Not Do A Thing Already Done."

Last edited by sundialsvcs; 03-15-2017 at 11:32 AM.
 
Old 03-15-2017, 12:09 PM   #5
camp0
Member
 
Registered: Dec 2016
Location: Dublin
Distribution: Fedora
Posts: 70

Rep: Reputation: 4
It looks that you have a list with two elements under TagX

so probably you need to do something like

Code:
print jaysonData["Tag1"]["TagX"][0]["TagC"]
 
Old 03-17-2017, 06:20 AM   #6
slimcharles
LQ Newbie
 
Registered: Dec 2016
Distribution: Ubuntu 20.04 & Debian 11
Posts: 13

Original Poster
Rep: Reputation: Disabled
I asked the same question in stackoverflow.
And This answer solved my problem. For future references I am posting the answer and the link here.
Thanks everyone for their help and time.

http://stackoverflow.com/a/42803904/7672271

Answer:
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:
Code:
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:
Code:
print [x['TagB'] for x in tag1JaysonData['TagX']]
 
2 members found this post helpful.
Old 03-17-2017, 09:04 AM   #7
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,249

Rep: Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323
Quote:
Originally Posted by slimcharles View Post
In the returned JSON, the value of Tag1 is a string, not more JSON.
Okay, I can see that now that it's been pointed out.

From your first post:

Code:
{
	"Tag1":"{
Code:
"date": "10.03.2017 21:00:00"
	}"
Pasting it into jsonlint.com actually produces a "parse error".

If you have control over the JSON, then you can fix it:

Code:
{
	"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"
	}
}
I see in your comments that you don't have control over the data returned. Which makes me wonder (as the SO commenters did) which service is returning this bad JSON.

Last edited by dugan; 03-17-2017 at 09:10 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What's a good language to use for polling WEB API's and parsing JSON results? usao Programming 15 08-08-2016 07:52 PM
[SOLVED] Parsing Javascript To JSON Using Python 3 cin_ Programming 3 05-18-2015 04:08 PM
[SOLVED] Parsing Javascript To JSON cin_ Programming 5 05-18-2015 04:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration