Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prettify JSON from curl in OSX

I'm trying to prettify JSON response from curl, but it doesn't work.

Narnia:~ vitaly$ curl -s https://api.vk.com/method/photos.getAlbums?uid=6015870&access_token=0275127e915981fe795840165e532169482cbdfc4ca1dbd48619a687a65fead88c468cdafe9743e231c37 | python -m json.tool
[4] 8822
No JSON object could be decoded
Narnia:~ vitaly$ {"response":[{"aid":"109967643","thumb_id":"163950716","owner_id":"6015870","title":"9 мая 2010","description":"","created":"1273613868","updated":"1273695901","size":7},{"aid":"95299056","thumb_id":"135702541","owner_id":"6015870","title":"Kemer 2009","description":"http:\/\/picasaweb.google.com.ua\/Ivanov.Vitalii\/Kemer2009","created":"1250355164","updated":"1250520619","size":72},{"aid":"43368938","thumb_id":"116630327","owner_id":"6015870","title":"Лето 2008 (Алупка)","description":"","created":"1220478168","updated":"1221348162","size":43},{"aid":"38630587","thumb_id":"116886016","owner_id":"6015870","title":"Flowers","description":"","created":"1217680400","updated":"1236774230","size":9},{"aid":"36658103","thumb_id":"163954451","owner_id":"6015870","title":"Моя ката","description":"","created":"1216419744","updated":"1273698620","size":8},{"aid":"23100962","thumb_id":"112723283","owner_id":"6015870","title":"Только пипл","description":"","created":"1208636545","updated":"1210382181","size":9},{"aid":"15473894","thumb_id":"114370266","owner_id":"6015870","title":"other","description":"","created":"1203516879","updated":"1327679223","size":29},{"aid":"15471241","thumb_id":"95266020","owner_id":"6015870","title":"Сам батя или чего нельзя в фотошопе =)","description":"","created":"1203516081","updated":"1203516728","size":4}]}
[4]   Done                    curl -s https://api.vk.com/method/photos.getAlbums?uid=6015870
Narnia:~ vitaly$ 

Why do I receive "No JSON object could be decoded"? Url I'm requesting always returns valid json. If I pass json from response manually like $ echo jsonreponse | python -m json.tool, it does prettify json.

Am I doing something wrong ?

like image 349
Madman Avatar asked Mar 24 '13 11:03

Madman


People also ask

How do I get JSON with Curl?

To get JSON with Curl, you need to make an HTTP GET request and provide the Accept: application/json request header. The application/json request header is passed to the server with the curl -H command-line option and tells the server that the client is expecting JSON in response.

What is Json_pp?

json_pp converts between some input and output formats (one of them is JSON). This program was copied from json_xs and modified. The default input format is json and the default output format is json with pretty option.


1 Answers

You need to put quotes around your URL:

curl -s "https://api.vk.com/method/photos.getAlbums?uid=6015870&access_token=0275127e915981fe795840165e532169482cbdfc4ca1dbd48619a687a65fead88c468cdafe9743e231c37" | python -m json.tool

The & character is a shell meta character, it puts the command in the background. As a result you are not passing the full URL to the server but put the command curl -s https://api.vk.com/method/photos.getAlbums?uid=6015870 in the background instead.

This is why the shell echos [4] 8822 to you; you put a fourth job so far into the jobs queue, it was given the PID 8822.

The remaining command is then:

access_token=0275127e915981fe795840165e532169482cbdfc4ca1dbd48619a687a65fead88c468cdafe9743e231c37 | python -m json.tool

which will not produce valid JSON indeed:

$ access_token=0275127e915981fe795840165e532169482cbdfc4ca1dbd48619a687a65fead88c468cdafe9743e231c37 | python -m json.tool
No JSON object could be decoded

You can see the job you put into the background complete on the next line:

Narnia:~ vitaly$ {"response":[{"aid":"109967643","thumb_id":"163950716","owner_id":"6015870","title":"9 мая 2010","description":"","created":"1273613868","updated":"1273695901","size":7},{"aid":"95299056","thumb_id":"135702541","owner_id":"6015870","title":"Kemer 2009","description":"http:\/\/picasaweb.google.com.ua\/Ivanov.Vitalii\/Kemer2009","created":"1250355164","updated":"1250520619","size":72},{"aid":"43368938","thumb_id":"116630327","owner_id":"6015870","title":"Лето 2008 (Алупка)","description":"","created":"1220478168","updated":"1221348162","size":43},{"aid":"38630587","thumb_id":"116886016","owner_id":"6015870","title":"Flowers","description":"","created":"1217680400","updated":"1236774230","size":9},{"aid":"36658103","thumb_id":"163954451","owner_id":"6015870","title":"Моя ката","description":"","created":"1216419744","updated":"1273698620","size":8},{"aid":"23100962","thumb_id":"112723283","owner_id":"6015870","title":"Только пипл","description":"","created":"1208636545","updated":"1210382181","size":9},{"aid":"15473894","thumb_id":"114370266","owner_id":"6015870","title":"other","description":"","created":"1203516879","updated":"1327679223","size":29},{"aid":"15471241","thumb_id":"95266020","owner_id":"6015870","title":"Сам батя или чего нельзя в фотошопе =)","description":"","created":"1203516081","updated":"1203516728","size":4}]}
[4]   Done                    curl -s https://api.vk.com/method/photos.getAlbums?uid=6015870

With the quotes the command produces:

$ curl -s "https://api.vk.com/method/photos.getAlbums?uid=6015870&access_token=0275127e915981fe795840165e532169482cbdfc4ca1dbd48619a687a65fead88c468cdafe9743e231c37" | python -m json.tool
{
    "error": {
        "error_code": 5, 
        "error_msg": "User authorization failed: access_token was given to another ip address.", 
        "request_params": [
            {
                "key": "oauth", 
                "value": "1"
            }, 
            {
                "key": "method", 
                "value": "photos.getAlbums"
            }, 
            {
                "key": "uid", 
                "value": "6015870"
            }, 
            {
                "key": "access_token", 
                "value": "0275127e915981fe795840165e532169482cbdfc4ca1dbd48619a687a65fead88c468cdafe9743e231c37"
            }
        ]
    }
}

Presumably for you it'll produce more useful information as clearly I don't have the right access token for my IP address. :-)

like image 133
Martijn Pieters Avatar answered Nov 03 '22 09:11

Martijn Pieters