Read a JSON from a URL using Python 3 - python

I need an help on reading a JSON from a URL, which has the below JSON in it:
{
"totalItems":2,
"#href":"/classes/dsxplan:Program",
"#id":"dsxplan:Program",
"#mask":"dsplan:MVMask.WorkPackage.Complex",
"#type":"Collection",
"#code":200,
"#context":{
"dsxplan":"xplan",
"dsplan":"plan",
"dspol":"pol",
"image":{
"#id":"dspol:image",
"#type":"#id"
},
"dskern":"kern"
},
"member":[
{
"dsplan:actualType":{
"#href":"/resources/dsxplan:Program",
"#id":"dsxplan:Program",
"#mask":"dskern:Mask.Default",
"image":"iconProgram.png"
},
"dskern:owner":{
"#href":"/resources/dskern:Person.Creator",
"#id":"dskern:Person.Creator",
"#mask":"dskern:MVMask.Person.Complex",
"dsplan:actualType":{
"#href":"/resources/foaf:Person",
"#id":"foaf:Person",
"#mask":"dskern:Mask.Default"
}
},
"dspol:modificationDate":"2017-09-08T17:54:36.786Z",
"#href":"/resources/dsxplan:DSLCProgram.R-399",
"#id":"dsxplan:DSLCProgram.R-399",
"#mask":"dsplan:MVMask.WorkPackage.Complex",
"#etag":"7412df19-1dde-4245-b40b-5dd86dbbe3f1"
},
{
"dsplan:actualType":{
"#href":"/resources/dsxplan:Program",
"#id":"dsxplan:Program",
"#mask":"dskern:Mask.Default",
"image":"iconProgram.png"
},
"dskern:owner":{
"#href":"/resources/dskern:Person.Creator",
"#id":"dskern:Person.Creator",
"#mask":"dskern:MVMask.Person.Complex",
"dsplan:actualType":{
"#href":"/resources/foaf:Person",
"#id":"foaf:Person",
"#mask":"dskern:Mask.Default"
}
},
"dspol:modificationDate":"2017-09-08T17:54:36.786Z",
"#href":"/resources/dsxplan:xComModel2017program.R-394",
"#id":"dsxplan:xComModel2017program.R-394",
"#mask":"dsplan:MVMask.WorkPackage.Complex",
"#etag":"7412df19-1dde-4245-b40b-5dd86dbbe3f1"
}
]
}
I just need to read this json from a link provided. I tried the below code:
import urllib.request
request= urllib.request.Request("https://dummy_link")
response = urllib.request.urlopen(request)
input = (response.read().decode('utf-8'))
json.loads(input)
This code throws me this error:
"JSONDecodeError: Expecting value: line 9 column 1 (char 12)"
Could you please help me get this right? I really appreciate the help.!!

You could use Requests library which is more simple than urllib:
For instance:
import requests
r = requests.get('https://dummy_link')
obj = r.json()
EDIT
If you want to use urllib, you can do as below:
import urllib.request
import json
with urllib.request.urlopen("https://dummy_link") as f:
content = f.read()
obj = json.loads(content)
There is no need to convert the binary content to unicode string.
There is an urllib howto in the official documentation.

Related

python : get specific value from json string

I Need to Exact Value from Json response
how can I get a specific json value python ???
this is my Json string
I need to extract Id value
"window.__store__ ="{
"listingReducer":{
"selectedListing":{
"id":2234588,
"has_video":0,
"refresh":1625551240,
"category":6,
"ketchen":1,
"lift":1,
"livings":1,
"maid":null,
"meter_price":null,
"playground":null,
"location":{
"lat":26.378031,
"lng":50.124866
},
"views":6075,
},3
}
}
this my python code :
import json
from selenium import webdriver
jsonScript =driver.find_element_by_xpath("/html/body/script[6]")
jsonText = jsonScript.get_attribute('innerHTML')
.
.
.
json_str = json.dumps(jsonText)
resp = json.loads(json_str)
#print (resp)
print(resp[0]['listingReducer']['selectedListing']['id'])
.
.
.
And this is the Error
TypeError: string indices must be integers
Your string is not in JSON format.
I have managed to convert it into JSON by removing irrelevant content (that doesn't follow JSON encoding).
This code will get you the ID from the JSON string.
import json
s = '''
{
"listingReducer":{
"selectedListing":{
"id":2234588,
"has_video":0,
"refresh":1625551240,
"category":6,
"ketchen":1,
"lift":1,
"livings":1,
"maid":null,
"meter_price":null,
"playground":null,
"location":{
"lat":26.378031,
"lng":50.124866
},
"views":6075
}
}
}
'''
d = json.loads(s)
print(f"ID: {d['listingReducer']['selectedListing']['id']}")
Output:
ID: 2234588

TypeError: string indices must be integers - JSON Python

So I'm, getting the error:
Traceback (most recent call last):
File "C:/Users/User/PycharmProjects/db/dm testing.py", line 12, in <module>
for id in ids['id']:
TypeError: string indices must be integers
With the following code:
import requests
import json
token = 'mytoken'
headers = {
'Authorization': token,
}
r = requests.get('https://canary.discordapp.com/api/v6/users/#me/channels', headers=headers).json()
ids=json.dumps(r)
for id in ids['id']:
print(id)
The JSON looks like this:
[
{
"id": "771634716042461195",
},
{
"id": "771654126345781278",
},
{
"id": "771658044526034967",
}
]
I want to print out the channel id's, which in this case are:
771634716042461195 and 771654126345781278.
Any help greatly appreciated.
Ok, so I found the solution, here is my final code:
import requests
import json
token = 'mytoken'
headers = {
'Authorization': token,
}
r = requests.get('https://canary.discordapp.com/api/v6/users/#me/channels', headers=headers).json()
x = 0
for i in range(len(r)):
print(r[x]['id'])
x+=1
It prints all the ID's of the channels. Hopefully this can help some other people out.
You are converting your parsed json back into a string, but you want it to be a dictionary, not a string. You can completely remove the second line.
r = requests.get('https://canary.discordapp.com/api/v6/users/#me/channels', headers=headers).json()
for id in r['id']:
print(id)

How to print json info with python?

I have a json (url = http://open.data.amsterdam.nl/ivv/parkeren/locaties.json) and I want to print all 'title', 'adres', 'postcode'. How can I do that?
I want to print it like this:
title.
adres.
postcode.
title.
adres.
postcode.
so among themselves
I hope you can help me with this
import urllib, json
url = "http://open.data.amsterdam.nl/ivv/parkeren/locaties.json"
import requests
search = requests.get(url).json()
print(search['title'])
print(search['adres'])
print(search['postcode'])
Using print(json.dumps(r, indent=4)) you can see that the structure is
{
"parkeerlocaties": [
{
"parkeerlocatie": {
"title": "Fietsenstalling Tolhuisplein",
"Locatie": "{\"type\":\"Point\",\"coordinates\":[4.9032801,52.3824545]}",
...
}
},
{
"parkeerlocatie": {
"title": "Fietsenstalling Paradiso",
"Locatie": "{\"type\":\"Point\",\"coordinates\":[4.8833735,52.3621851]}",
...
}
},
So to access the inner properties, you need to follow the JSON path
import requests
url = ' http://open.data.amsterdam.nl/ivv/parkeren/locaties.json'
search = requests.get(url).json()
for parkeerlocatie in search["parkeerlocaties"]:
content = parkeerlocatie['parkeerlocatie']
print(content['title'])
print(content['adres'])
print(content['postcode'])
print()

parsing and getting list from response of get request

I'm trying to parse a website with the requests module:
import requests
some_data = {'a':'',
'b':''}
with requests.Session() as s:
result = s.post('http://website.com',data=some_data)
print(result.text)
The page is responding as below:
{
"arrangetype":"U",
"list": [
{
"product_no":43,
"display_order":4,
"is_selling":"T",
"product_empty":"F",
"fix_position":null,
"is_auto_sort":false
},
{
"product_no":44,
"display_order":6,
"is_selling":"T",
"product_empty":"F",
"fix_position":null,
"is_auto_sort":false
}
],
"length":2
}
I found that instead of parsing full HTML, it would be better to deal with the response as all the data I want is in that response.
What I want to get is a list of the values of product_no, so the expected result is:
[43,44]
How do I do this?
Convert your JSON response to a dictionary with json.loads(), and collect your results in a list comprehension.
Demo:
from json import loads
data = """{
"arrangetype":"U",
"list": [
{
"product_no":43,
"display_order":4,
"is_selling":"T",
"product_empty":"F",
"fix_position":null,
"is_auto_sort":false
},
{
"product_no":44,
"display_order":6,
"is_selling":"T",
"product_empty":"F",
"fix_position":null,
"is_auto_sort":false
}
],
"length":2
}"""
json_dict = loads(data)
print([x['product_no'] for x in json_dict['list']])
# [43, 44]
Full Code:
import requests
from json import loads
some_data = {'a':'',
'b':''}
with requests.Session() as s:
result = s.post('http://website.com',data=some_data)
json_dict = loads(result.text)
print([x["product_no"] for x in json_dict["list"]])

Extract data from JSON API using Python [duplicate]

This question already has answers here:
How can I extract a single value from a nested data structure (such as from parsing JSON)?
(5 answers)
Closed 4 years ago.
I go through this part:
How do I extract the data from that URL? I only want to print out the "networkdiff": 58954.60268219.
from urllib import urlopen
url = urlopen('http://21.luckyminers.com/index.php?page=api&action=getpoolstatus&api_key=8dba7050f9fea1e6a554bbcf4c3de5096795b253b45525c53562b72938771c41').read()
print url
This is what the API display as a result from print url command:
{
"getpoolstatus": {
"version": "1.0.0",
"runtime": 16.618967056274,
"data": {
"pool_name": "21 Coin Pool # Luckyminers.com",
"hashrate": 485426748,
"efficiency": 98.1,
"workers": 14,
"currentnetworkblock": 12025,
"nextnetworkblock": 12026,
"lastblock": 12023,
"networkdiff": 58954.60268219,
"esttime": 521.61956775542,
"estshares": 241478052.58625,
"timesincelast": 427,
"nethashrate": 485426748
}
}
}
You can use the json module to parse out a Python dictionary and get right to the value like so:
import json
result = json.loads(url) # result is now a dict
print '"networkdiff":', result['getpoolstatus']['data']['networkdiff']
To do this multiple times (to answer your question in the comments section):
import json
import urllib
urls = {'joe': 'url1', 'jack': 'url2', 'jane': 'url3'}
for who in urls.keys():
url = urllib.urlopen(urls[who])
result = json.loads(url) # result is now a dict
print 'For %s: "networkdiff":' % who, result['getpoolstatus']['data']['networkdiff']
convert the response to json and then read it
from urllib import urlopen
import simplejson as json
url = urlopen('http://21.luckyminers.com/index.php?page=api&action=getpoolstatus&api_key=8dba7050f9fea1e6a554bbcf4c3de5096795b253b45525c53562b72938771c41').read()
url = json.loads(url)
print url.get('getpoolstatus').get('data').get('networkdiff')

Categories