0

Tonight I want to make a request to the topsy.com API, which returns JSON and render that as HTML.

I've never done anything with json before since it's impossible to read in the browser, but that's the only format Topsy returns, so I'm trying to learn what I can. I have a very basic question:

  1. Why do some APIs make me download a json file when I ping them in the browser while others show me a white screen with a bunch of json text on them?

Compare the result of clicking this URL:

http://otter.topsy.com/search.json?q=site:wired.com&window=d7

with this one:

http://www.twitter.com/statuses/user_timeline/nirmalpatel.json?callback=showTweets&count=20

What's the difference?

I ask because I found some js that will render json as HTML

http://nirmalpatel.com/json2posh/

In his example, he makes a call to Twitter's API that returns JSON, runs that through his javascript, and then it renders HTML on the page.

I'm trying to figure out how to change his code to make it work for the Topsy API call.

Here's the API request I want to render as HTML

http://otter.topsy.com/search.json?q=site:wired.com&window=d7

I've added his json2posh.js to my site, so that's "live" so to speak.

And I've added the callback script and the DOM element where the HTML is supposed to be rendered.

What I can't figure out is how I'm supposed to call the Topsy API and then feed the response into this guy's code.

I'm thinking it might be explained by whatever reason those two URLS show me such different results.

bflora2
  • 733
  • 3
  • 8
  • 26

1 Answers1

1

if the site returns json as content-type: text/plain you will see it in the browser.

if the site returns json as content-type: text/json you will likely get a save file dialog.

if you are using IE, you can force it to show json as text: see How can I convince IE to simply display application/json rather than offer to download it?

firefox has several JSON viewer addons but I am not familiar with them.

Community
  • 1
  • 1
Sky Sanders
  • 36,396
  • 8
  • 69
  • 90
  • the json2posh.js utility works with an API call that returns text/plain in its example. Do I need to change something to get it to work with a text/json-style API? – bflora2 Oct 04 '10 at 02:53
  • @bflora - i am not familiar with the utility but it is unlikely that it will differentiate content-types. i.e. it should consume both equally. – Sky Sanders Oct 04 '10 at 03:04
  • @bflora - i am right in the middle of something but that utility looks interesting. I may be able to take a closer look later this evening when things quiet down. – Sky Sanders Oct 04 '10 at 03:05
  • That'd be great. I duplicated the example just fine. The example runs a call to Twitter's JSON API, runs the returned JSON object through his JS which renders it as HTML. – bflora2 Oct 04 '10 at 03:23
  • I'm new to js and JSON so what I can't figure out is how I'm supposed to call the TOPSY.com API and how/if I need to edit the callback function to make it work properly. – bflora2 Oct 04 '10 at 03:23
  • @bflora - sorry- caught a contract last night and am pretty busy at the moment, I will try to give this some attention tonight. – Sky Sanders Oct 05 '10 at 04:36
  • Hi, Sky. I think I figured this out by just doing it in php rather than VIA javascript. Took me a while longer to get the hang of it, but I figured it out! – bflora2 Oct 11 '10 at 08:20