1

When I call this url: https://dogsatmyjob.tumblr.com/api/read/json?id=165570102911

I get a string response like this:

var tumblr_api_read = {"tumblelog":{"title":"Dogs At My Job","description":"<p>Just a simple collection of the pups that I see everyday at my dog care job.<\/p>","name":"dogsatmyjob","timezone":"US\/Mountain","cname":false,"feeds":[]},"posts-start":0,"posts-total":1359,"posts-type":false,"posts":[{"id":"165570102911","url":"https:\/\/dogsatmyjob.tumblr.com\/post\/165570102911","url-with-slug":"https:\/\/dogsatmyjob.tumblr.com\/post\/165570102911\/dogsatmyjob-catcher-is-always-camera-ready","type":"photo","date-gmt":"2017-09-21 04:00:40 GMT","date":"Wed, 20 Sep 2017 22:00:40","bookmarklet":0,"mobile":0,"feed-item":"","from-feed-id":0,"unix-timestamp":1505966440,"format":"html","reblog-key":"FpmqGk7D","slug":"dogsatmyjob-catcher-is-always-camera-ready","is-submission":false,"like-button":"<div class=\"like_button\" data-post-id=\"165570102911\" data-blog-name=\"dogsatmyjob\" id=\"like_button_165570102911\"><iframe id=\"like_iframe_165570102911\" src=\"https:\/\/assets.tumblr.com\/assets\/html\/like_iframe.html?_v=66c22ab5319d742bca5762b8d18f9d06#name=dogsatmyjob&amp;post_id=165570102911&amp;color=black&amp;rk=FpmqGk7D&amp;root_id=159690206439\" scrolling=\"no\" width=\"20\" height=\"20\" frameborder=\"0\" class=\"like_toggle\" allowTransparency=\"true\" name=\"like_iframe_165570102911\"><\/iframe><\/div>","reblog-button":"<a href=\"https:\/\/www.tumblr.com\/reblog\/165570102911\/FpmqGk7D\" 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>","state":"published","note-count":"219","reblogged-from-url":"https:\/\/dogsatmyjob.tumblr.com\/post\/159690206439\/catcher-is-always-camera-ready","reblogged-from-name":"dogsatmyjob","reblogged-from-title":"Dogs At My Job","reblogged_from_avatar_url_512":"https:\/\/66.media.tumblr.com\/avatar_89ca7a2e202d_512.png","reblogged_from_avatar_url_128":"https:\/\/66.media.tumblr.com\/avatar_89ca7a2e202d_128.png","reblogged_from_avatar_url_96":"https:\/\/66.media.tumblr.com\/avatar_89ca7a2e202d_96.png","reblogged_from_avatar_url_64":"https:\/\/66.media.tumblr.com\/avatar_89ca7a2e202d_64.png","reblogged_from_avatar_url_48":"https:\/\/66.media.tumblr.com\/avatar_89ca7a2e202d_48.png","reblogged_from_avatar_url_40":"https:\/\/66.media.tumblr.com\/avatar_89ca7a2e202d_40.png","reblogged_from_avatar_url_30":"https:\/\/66.media.tumblr.com\/avatar_89ca7a2e202d_30.png","reblogged_from_avatar_url_24":"https:\/\/66.media.tumblr.com\/avatar_89ca7a2e202d_24.png","reblogged_from_avatar_url_16":"https:\/\/66.media.tumblr.com\/avatar_89ca7a2e202d_16.png","reblogged-root-url":"https:\/\/dogsatmyjob.tumblr.com\/post\/159690206439\/catcher-is-always-camera-ready","reblogged-root-name":"dogsatmyjob","reblogged-root-title":"Dogs At My Job","reblogged_root_avatar_url_512":"https:\/\/66.media.tumblr.com\/avatar_89ca7a2e202d_512.png","reblogged_root_avatar_url_128":"https:\/\/66.media.tumblr.com\/avatar_89ca7a2e202d_128.png","reblogged_root_avatar_url_96":"https:\/\/66.media.tumblr.com\/avatar_89ca7a2e202d_96.png","reblogged_root_avatar_url_64":"https:\/\/66.media.tumblr.com\/avatar_89ca7a2e202d_64.png","reblogged_root_avatar_url_48":"https:\/\/66.media.tumblr.com\/avatar_89ca7a2e202d_48.png","reblogged_root_avatar_url_40":"https:\/\/66.media.tumblr.com\/avatar_89ca7a2e202d_40.png","reblogged_root_avatar_url_30":"https:\/\/66.media.tumblr.com\/avatar_89ca7a2e202d_30.png","reblogged_root_avatar_url_24":"https:\/\/66.media.tumblr.com\/avatar_89ca7a2e202d_24.png","reblogged_root_avatar_url_16":"https:\/\/66.media.tumblr.com\/avatar_89ca7a2e202d_16.png","tumblelog":{"title":"Dogs At My Job","name":"dogsatmyjob","cname":false,"url":"https:\/\/dogsatmyjob.tumblr.com\/","timezone":"US\/Mountain","avatar_url_512":"https:\/\/66.media.tumblr.com\/avatar_89ca7a2e202d_512.png","avatar_url_128":"https:\/\/66.media.tumblr.com\/avatar_89ca7a2e202d_128.png","avatar_url_96":"https:\/\/66.media.tumblr.com\/avatar_89ca7a2e202d_96.png","avatar_url_64":"https:\/\/66.media.tumblr.com\/avatar_89ca7a2e202d_64.png","avatar_url_48":"https:\/\/66.media.tumblr.com\/avatar_89ca7a2e202d_48.png","avatar_url_40":"https:\/\/66.media.tumblr.com\/avatar_89ca7a2e202d_40.png","avatar_url_30":"https:\/\/66.media.tumblr.com\/avatar_89ca7a2e202d_30.png","avatar_url_24":"https:\/\/66.media.tumblr.com\/avatar_89ca7a2e202d_24.png","avatar_url_16":"https:\/\/66.media.tumblr.com\/avatar_89ca7a2e202d_16.png"},"photo-caption":"<p><a href=\"https:\/\/dogsatmyjob.tumblr.com\/post\/159690206439\/catcher-is-always-camera-ready\" class=\"tumblr_blog\">dogsatmyjob<\/a>:<\/p>\n\n<blockquote><p>Catcher is always camera-ready.<\/p><\/blockquote>","width":960,"height":1280,"photo-url-1280":"https:\/\/66.media.tumblr.com\/cac87bd90ea04d2ed28194aed7a79308\/tumblr_oojzetRpQD1w71lqmo1_1280.jpg","photo-url-500":"https:\/\/66.media.tumblr.com\/cac87bd90ea04d2ed28194aed7a79308\/tumblr_oojzetRpQD1w71lqmo1_500.jpg","photo-url-400":"https:\/\/66.media.tumblr.com\/cac87bd90ea04d2ed28194aed7a79308\/tumblr_oojzetRpQD1w71lqmo1_400.jpg","photo-url-250":"https:\/\/66.media.tumblr.com\/cac87bd90ea04d2ed28194aed7a79308\/tumblr_oojzetRpQD1w71lqmo1_250.jpg","photo-url-100":"https:\/\/66.media.tumblr.com\/cac87bd90ea04d2ed28194aed7a79308\/tumblr_oojzetRpQD1w71lqmo1_100.jpg","photo-url-75":"https:\/\/66.media.tumblr.com\/cac87bd90ea04d2ed28194aed7a79308\/tumblr_oojzetRpQD1w71lqmo1_75sq.jpg","photos":[],"tags":["catcher","german shepherd","germanshepherd","cute","adorable","beautiful","dog","dogs","puppy","puppies","dogsatmyjob","dogblr"]}]};

The thing is that this is a string of an asignment of a json body. Not just that, the / are shown as \/, so I would need to parse it but also escape these characters.

What would be the best way of parsing this so I can treat it as an object / array ?

1
  • My best bet would be to just use response.body.replace("var tumblr_api_read = ", "") and then pop the last ; character and then using JSON.parse() ? I wanted to find a better solution... Commented May 29, 2020 at 14:47

1 Answer 1

2

The API documentation expects you to access a URL like that as the source of a <script> tag. That way the code returned will create the tumblr_api_read global variable.

What you could do when reading via an xhr would be to make a Function instance from it with an additional return statement:

var tumblrObject = new Function(responseString + " return tumblr_api_read;")();

Alternatively, you could chop off the trailing semicolon and the var declaration with some string hackery and then pass what's left to JSON.parse(). The backslashes before the slashes won't cause a problem; that's actually part of the JSON standard.

I should add that using the new Function() approach would introduce a risk. If tumblr.com is hacked, the API could return code that attempted to do bad things. That risk is present when using the <script> tag approach too. Modifying the string and using JSON.parse() is probably preferable from that standpoint.

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

1 Comment

If I understood it correctly, the variable returned within the context of a function then it can be returned when invoked. Awesome sir, thanks. It works.

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.