2

I am using the tumblr api to get data for a certain user, and the response is served as js rather then json:

https://taylorswift.tumblr.com/api/read?start=0&format=json&num=1

var tumblr_api_read = {"tumblelog":{"title":"Taylor Swift","description":"","name":"taylorswift","timezone":"US\/Eastern","cname":false,"feeds":[]},"posts-start":0,"posts-total":186,"posts-type":false,"posts":[{"id":"172426031730","url":"http:\/\/taylorswift.tumblr.com\/post\/172426031730","url-with-slug":"http:\/\/taylorswift.tumblr.com\/post\/172426031730\/did-you-see-what-hayley-kiyoko-said-about-taylor","type":"answer","date-gmt":"2018-03-30 22:27:43 GMT","date":"Fri, 30 Mar 2018 18:27:43","bookmarklet":0,"mobile":0,"feed-item":"","from-feed-id":0,"unix-timestamp":1522448863,"format":"html","reblog-key":"GJl0OUt4","slug":"did-you-see-what-hayley-kiyoko-said-about-taylor","is-submission":false,"like-button":"<div class=\"like_button\" data-post-id=\"172426031730\" data-blog-name=\"taylorswift\" id=\"like_button_172426031730\"><iframe id=\"like_iframe_172426031730\" src=\"https:\/\/assets.tumblr.com\/assets\/html\/like_iframe.html?_v=fc298e85f978b8662a643fe0a6b8c638#name=taylorswift&amp;post_id=172426031730&amp;color=black&amp;rk=GJl0OUt4&amp;root_id=172423870366\" scrolling=\"no\" width=\"20\" height=\"20\" frameborder=\"0\" class=\"like_toggle\" allowTransparency=\"true\" name=\"like_iframe_172426031730\"><\/iframe><\/div>","reblog-button":"<a href=\"https:\/\/www.tumblr.com\/reblog\/172426031730\/GJl0OUt4\" class=\"reblog_button\"style=\"display: block;width:20px;height:20px;\"><svg width=\"100%\" height=\"100%\" viewBox=\"0 0 21 21\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" xmlns:xlink=\"http:\/\/www.w3.org\/1999\/xlink\" fill=\"#000\"><path d=\"M5.01092527,5.99908429 L16.0088498,5.99908429 L16.136,9.508 L20.836,4.752 L16.136,0.083 L16.1360004,3.01110845 L2.09985349,3.01110845 C1.50585349,3.01110845 0.979248041,3.44726568 0.979248041,4.45007306 L0.979248041,10.9999998 L3.98376463,8.30993634 L3.98376463,6.89801007 C3.98376463,6.20867902 4.71892527,5.99908429 5.01092527,5.99908429 Z\"><\/path><path d=\"M17.1420002,13.2800293 C17.1420002,13.5720293 17.022957,14.0490723 16.730957,14.0490723 L4.92919922,14.0490723 L4.92919922,11 L0.5,15.806 L4.92919922,20.5103758 L5.00469971,16.9990234 L18.9700928,16.9990234 C19.5640928,16.9990234 19.9453125,16.4010001 19.9453125,15.8060001 L19.9453125,9.5324707 L17.142,12.203\"><\/path><\/svg><\/a>","note-count":"24565","reblogged-from-url":"http:\/\/shaking-off-the-bad-blood.tumblr.com\/post\/172423870366\/did-you-see-what-hayley-kiyoko-said-about-taylor","reblogged-from-name":"shaking-off-the-bad-blood","reblogged-from-title":"\"Samar, right?\"","reblogged_from_avatar_url_16":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_16.png","reblogged_from_avatar_url_24":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_24.png","reblogged_from_avatar_url_30":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_30.png","reblogged_from_avatar_url_40":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_40.png","reblogged_from_avatar_url_48":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_48.png","reblogged_from_avatar_url_64":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_64.png","reblogged_from_avatar_url_96":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_96.png","reblogged_from_avatar_url_128":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_128.png","reblogged_from_avatar_url_512":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_512.png","reblogged-root-url":"http:\/\/shaking-off-the-bad-blood.tumblr.com\/post\/172423870366\/did-you-see-what-hayley-kiyoko-said-about-taylor","reblogged-root-name":"shaking-off-the-bad-blood","reblogged-root-title":"\"Samar, right?\"","reblogged_root_avatar_url_16":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_16.png","reblogged_root_avatar_url_24":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_24.png","reblogged_root_avatar_url_30":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_30.png","reblogged_root_avatar_url_40":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_40.png","reblogged_root_avatar_url_48":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_48.png","reblogged_root_avatar_url_64":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_64.png","reblogged_root_avatar_url_96":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_96.png","reblogged_root_avatar_url_128":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_128.png","reblogged_root_avatar_url_512":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_512.png","tumblelog":{"title":"Taylor Swift","name":"taylorswift","cname":false,"url":"http:\/\/taylorswift.tumblr.com\/","timezone":"US\/Eastern","avatar_url_16":"https:\/\/78.media.tumblr.com\/avatar_c4f07fd82125_16.png","avatar_url_24":"https:\/\/78.media.tumblr.com\/avatar_c4f07fd82125_24.png","avatar_url_30":"https:\/\/78.media.tumblr.com\/avatar_c4f07fd82125_30.png","avatar_url_40":"https:\/\/78.media.tumblr.com\/avatar_c4f07fd82125_40.png","avatar_url_48":"https:\/\/78.media.tumblr.com\/avatar_c4f07fd82125_48.png","avatar_url_64":"https:\/\/78.media.tumblr.com\/avatar_c4f07fd82125_64.png","avatar_url_96":"https:\/\/78.media.tumblr.com\/avatar_c4f07fd82125_96.png","avatar_url_128":"https:\/\/78.media.tumblr.com\/avatar_c4f07fd82125_128.png","avatar_url_512":"https:\/\/78.media.tumblr.com\/avatar_c4f07fd82125_512.png"},"question":"Did you see what Hayley Kiyoko said about Taylor? I&rsquo;m sad :(","answer":"<p>I did! I think people are actually misinterpreting the quote. I don\u2019t know if it is being taken out of context or what, but if you read the interview itself, I don\u2019t believe she was calling Taylor unoriginal or boy crazy. She was just defending her choice for wanting to make videos with female love interests. <\/p>\n\n<p>It is actually very similar to when Taylor back in 2014\/2015 would say in interviews how she believes people are sexist when they call her out for writing about her exes but don\u2019t call out people like Ed Sheeran or Bruno Mars for writing about theirs. She wasn\u2019t calling out Ed or Bruno at the time, yet rather criticizing how the media and the public handle the situation. Hayley appears to be doing the same.<\/p>"}]};

Then I want to get the data of tumblr_api_read, while I can use the eval to execute the scripts and get the return value, however I am afraid the eval is not safe.

So I wonder if there is an alternative way to get the data?

I have tried to js-unescape or html-unescape the data, none of that work.

5
  • The response after the assignment is in a totally JSON safe format, so simply extract the part after the equal and use JSON.parse. Commented Apr 11, 2018 at 10:21
  • 1
    Drop the var tumblr_api_read = prefix and parse the rest as JSON. Commented Apr 11, 2018 at 10:22
  • Consider using Tumblr API v2, which supports plain JSON, instead of v1. Commented Apr 11, 2018 at 10:25
  • @Bergi: I have tried that, check this: jsfiddle.net/g7jzmcbx Commented Apr 11, 2018 at 12:55
  • @hguser So, does it work? Commented Apr 11, 2018 at 14:52

2 Answers 2

4

From the documentation, I could only find a parameter debug. If passed with a value 1, it returns the JSON without prefixing it with var:

https://taylorswift.tumblr.com/api/read/json?start=0&num=1&debug=1

If that doesn't do, unfortunately, there isn't much left for this. You should switch to the latest API soon.

Sign up to request clarification or add additional context in comments.

Comments

0

If you just want to get the JSON out of it, you can do that:

JSON.parse( response.slice( response.indexOf('{') ) );

This find the first open-brace, gives you everything just without what's before it which is stringified JSON. So you just parse it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.