python Using urllib2 to retrieve JSON data

I need help retrieving a JSON object from a specific URL. I've provided the correct API key, but when I attempt to do the following:

data=json.loads(a.read())
print data

I encounter this error:

Traceback (most recent call last):
  File "C:\Python27\ArcGIS10.1\lib\bdb.py", line 387, in run
    exec cmd in globals, locals
  File "<module1>", line 1, in <module>
    from urllib2 import Request, urlopen
  File "C:\Python27\ArcGIS10.1\lib\json\__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "C:\Python27\ArcGIS10.1\lib\json\decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python27\ArcGIS10.1\lib\json\decoder.py", line 384, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

This is the code snippet in full:

from urllib2 import Request, urlopen
import urllib2
import json
apikey='abid'
url='https://search.onboard-apis.com/propertyapi/v1.0.0/sale/detail?address1=586+FRANKLIN+AVE&address2=brooklyn+NY+11238'
weburl=Request(url)
weburl.add_header('apikey',apikey)
a=urlopen(weburl)
data=json.loads(a.read())
print data

You can refer to the official Onboard APIs documentation for more information.

If you're facing a similar issue, check out this Stack Overflow question: Python urllib2: Receive JSON response from url

The output of `a.read()` looks like this:

<Response>
  ...

Answer №1

After reviewing the documentation link provided, it seems that you forgot to include the Accept header in your request.

The documentation specifies two mandatory headers:

  1. Accept - should be set to either
    • application/json
    • application/xml

Without the Accept header, the default response format is xml. To receive data in json format, ensure you set the Accept header to application/json.

weburl.add_header('Accept', 'application/json')

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to analyze the similarities and differences between the nodes and edges within two

There are graphs of Berlin from January 1, 2020 and January 1, 2021. How can I compare the changes in edges and nodes between the two maps? import osmnx as ox import pandas as pd import networkx as nx import matplotlib.pyplot as plt from itertools import ...

Encountering issues while web scraping from booking.com

Initially, I had the intention of visiting each hotel for: https://i.stack.imgur.com/HrrtX.png Unfortunately, there seems to be a JavaScript process required to open this subpage, and my script is unable to recognize its presence. Even with the correct U ...

Can you provide instructions for incorporating a binder badge into a document to make it appear as an image?

Creating interactive notebooks from repositories is possible on mybinder.org. You can generate a badge that serves as a link to launch the interactive notebook using mybinder. While this badge image is easily visible in the readme file of the repository, ...

Converting Markdown to HTML using AngularJS

I'm utilizing the Contentful API to retrieve content. It comes in the form of a JSON object to my Node server, which then forwards it to my Angular frontend. This JSON object contains raw markdown text that has not been processed yet. For instance, t ...

Transform legitimate JSON into a Task<string>

Currently, I am working on mocking ReadAsStringAsync on the first line to be used in a unit test that will result in a Task where the string represents the JSON provided below: var jsonString = await response.Content.ReadAsStringAsync(); // converting it ...

Encountering issues with deserializing a JSON instance while attempting to parse a JSON file using Jackson

The data I have is stored in a Json file: [ { "name":"Move", "$$hashKey":"object:79", "time":11.32818, "endTime":18.615535 }, { "name":"First Red Flash", "$$hashKey":"object:77", "time":15.749153 }, { ...

Challenges encountered in retrieving 'text' with Selenium in Python

After attempting to extract the list data from every drop-down menu on this page, I managed to access the 'li' tag section and retrieve the 'href' data using Selenium Python 3.6. However, I encountered an issue when trying to obtain the ...

Nodejs installation failure detected

I am dealing with a package node and I am looking to install it using the following command: ./configure --prefix=/path/NODEJS/node_installation && make && make install However, when I run this command, it returns the error below: File ...

Django: Utilizing AJAX to send a POST request for a form submission and updating the

Do you need assistance with creating a status view for your form? The current setup involves taking user input, submitting to a view where calculations are processed, and then redirecting to the output. However, the user must wait with no indication of pro ...

What could be causing my pygame screen to become unresponsive when awaiting user input?

I have recently started learning Python programming and am working on developing a game for my class project. However, whenever I run the program, the screen becomes unresponsive before allowing the user to input anything. Can anyone help me figure out wha ...

Learn how to showcase LINQ query results in a string array within an ASP.Net MVC view

I am working on integrating a tags input box into my application. After researching, I came across the library available at When it comes to the Controller Create Method: // GET: Posts/Create public ActionResult Create() { var Ta ...

The positioning of the button's rectangle is inaccurate

For my school project, I am working on an idle clicker game. One issue I encountered is that the button intended for upgrading clicking power has its rectangle placed incorrectly. The rectangle assigned to the button does not align with the actual button i ...

Merging two arrays to create a JSON object

column_names: [ "School Year Ending", "Total Students", "American Indian/Alaskan Native: Total", "American Indian/Alaskan Native: Male", "American Indian/Alaskan Native: Female", "Asian/Pacific Islander: Total", "Asian/Pacific I ...

Tips for efficiently looping through and making changes to pixel arrays using numpy

As a newcomer to Python and its libraries, I am exploring the conversion of HDR images to RGBM as outlined in WebGL Insights Chapter 16. import argparse import numpy import imageio import math # Handling arguments parser = argparse.ArgumentParser(descrip ...

Error encountered when iterating through a list: Stale Element Reference Exception

I'm currently working on creating a web scraper for this specific website. The main idea behind the code is to iterate through all institutions by selecting each institution's name (starting with 3B-Wonen), closing the pop-up window, clicking the ...

Is there a way to crack the Cloudflare turnstile CAPTCHA using Python, Selenium, and the 2Captcha API?

I am attempting to solve the cloudflare turnstile captcha on the website using Python Selenium with the 2captcha API. I followed the method shown on the 2captcha website, like this example solution for solving a cloudflare turnstile captcha: . However, I ...

Tips for dynamically changing the field name in a JSON object using Spark

Having a JSON log file with a JSON delimiter (/n), I am looking to convert it into Spark struct type. However, the first field name in every JSON varies within my text file. Is there a way to achieve this? val elementSchema = new StructType() .add("n ...

Receiving an Empty JSON Response

I am in the process of learning how to generate and return a Json object in C# using MVC controllers. This is my first time working with Json and I attempted to return an object, but it seems to be empty. public class A { private string name; pu ...

Discovering the characteristic values of a logarithmically transformed matrix

Currently I am tackling the challenge of determining the eigenvalues of real symmetric matrices that are poorly conditioned, with elements spanning a wide range from very small to very large values (ranging from 1e-8 to 1e15) and potentially negative. Thes ...

Posting a string using AJAX to the Java backend through JavaScript

I'm currently working on a comment section for a Web Client. There are two tables involved - one for the comments and another for the users. My goal is to send input data from HTML <input> tags to a Java Service where the data will be processed ...