Parse Yahoo! Web Service REST Calls

This HOWTO assumes that you already know how to make a Yahoo! Web Service request, and are wondering how to parse the XML or JSON returned by your request. This page contains these sections:

Parsing JSON

According to, JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. For more details about JSON, consult our JSON documentation.

The following example requires the org.json packages documented at The source for the org.json libraries can be found here.

Your Yahoo! Web Services request must include the output=json parameter so that it returns JSON. Not all Web services offered by Yahoo! return JSON. Consult the documentation for specifics. Here is a Web Service call returning JSON:

After retrieving a result set from a successful search request and saving the JSON response in a string, we can use the methods from the org.json package to parse the data.

// Construct a JSONObject from a source JSON text string.
// A JSONObject is an unordered collection of name/value pairs. Its external
// form is a string wrapped in curly braces with colons between the names
// and values, and commas between the values and names.
JSONObject jo = new JSONObject(jsonString); // A JSONArray is an ordered sequence of values. Its external form is a
// string wrapped in square brackets with commas between the values.
JSONArray ja; // Get the JSONObject value associated with the search result key.
jo = jo.getJSONObject("ResultSet"); // Get the JSONArray value associated with the Result key
ja = jo.getJSONArray("Result"); // Get the number of search results in this set
int resultCount = ja.length(); // Loop over each result and print the title, summary, and URL
for (int i = 0; i < resultCount; i++)
JSONObject resultObject = ja.getJSONObject(i);

The source code for this example can be found in the file

Parsing XML

Java offers a myriad of XML parsing options. You can take full control of a document via DOM or quickly and efficiently parse it with SAX. This example streamlines the access of Yahoo! Search Web Service XML results via XPath.

The sample search request:

String request = ""; 

The class DocumentBuilderFactory defines a factory API that enables applications to obtain a parser that produces DOM object trees from XML documents:

// Process response 
Document response = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(rstream);

XPath expressions are much more straight forward than navigating the DOM. Java 5 introduced the javax.xml.xpath package, an XML object-model independent library for querying documents with XPath. We'll use the XPathFactory class from the javax.xml.xpath package to parse the data:

XPathFactory factory = XPathFactory.newInstance();
XPath xPath=factory.newXPath();
//Get all search Result nodes
NodeList nodes = (NodeList)xPath.evaluate("/ResultSet/Result", response, XPathConstants.NODESET);
int nodeCount = nodes.getLength();
//iterate over search Result nodes
for (int i = 0; i < nodeCount; i++) {
  //Get each xpath expression as a string
  String title = (String)xPath.evaluate("Title", nodes.item(i), XPathConstants.STRING);
  String summary = (String)xPath.evaluate("Summary", nodes.item(i), XPathConstants.STRING);
  String url = (String)xPath.evaluate("Url", nodes.item(i), XPathConstants.STRING);
  //print out the Title, Summary, and URL for each search result
  System.out.println("Title: " + title);
  System.out.println("Summary: " + summary);
  System.out.println("URL: " + url);

The source code for this example can be found in the file

For More Information

For more information on using Java with Yahoo! Web Services APIs, see The Yahoo! Developer Network Java Developer Center.

Yahoo Forum Discussions