JSON-to-JSON Transformation

YQL transforms all JSON data sources into XML before returning results. To return JSON results that were obtained from a JSON data source, YQL must first transform the original JSON data to XML and then transform the XML back into a JSON result. During the tranformation from XML to JSON, the original JSON may be altered or become "lossy". In other words, the original JSON may not be the same as the returned JSON.

The original JSON may be altered in the following ways:

  • JSON numbers are returned as strings.
  • JSON arrays containing a single element are returned as a JSON object.

To prevent this "lossy" transformation, you append the query string parameter jsonCompat=new to the YQL Web Service URL that you are using. For those creating tables, you use the jsonCompat("new") when making REST calls to other Web services. To illustrate how the jsonCompat parameter is used, we'll look at the below examples that use the community table that queries the Google Books API, which only returns JSON.

  • Lossy JSON

    The following REST URI uses the public YQL Web Service URL and the Google Books table. The jsonCompat parameter is not added to the URI, so the original JSON returned from the Google Books API will be altered in the YQL response.

    https://query.yahooapis.com/v1/public/yql?q=SELECT * FROM google.books WHERE q="barack obama" AND maxResults=1&format=json&env=store://datatables.org/alltableswithkeys

    In the returned "lossy" JSON results below, notice that items is an object and that totalItems property has the the string value "958".

  • Lossless JSON

    Appending the jsonCompat=new query parameter to the REST URI as seen below, YQL now returns the same JSON data as the Gowalla API.

    https://query.yahooapis.com/v1/public/yql?q=SELECT * FROM google.books WHERE q="barack obama" AND maxResults=1&format=json&env=store://datatables.org/alltableswithkeys&jsonCompat=new

    In the returned "lossless" JSON results below, the items property is now an array containing objects as elements, and totalItems is a number.

Table of Contents