heldercorreia.com

autodidata, programador e consultor

Formatar JSON no terminal

Quando estou a desenvolver código que consome um serviço web, e já que tenho o terminal sempre aberto, gosto de verificar as respostas com curl.

O problema é que é difícil ler as respostas JSON sem estar indentado e com as quebras de linha, mas isso resolve-se facilmente em qualquer sistema com python instalado:

curl -s <url> | python -m json.tool

Antes

$ curl -s "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%3D26189893%20and%20u%3D'c'&format=json"
{"query":{"count":1,"created":"2014-11-04T13:54:37Z","lang":"en-US","results":{"channel":{"title":"Yahoo! Weather - Ponta Delgada, PT","link":"http://us.rd.yahoo.com/dailynews/rss/weather/Ponta_Delgada__PT/*http://weather.yahoo.com/forecast/POXX0020_c.html","description":"Yahoo! Weather for Ponta Delgada, PT","language":"en-us","lastBuildDate":"Tue, 04 Nov 2014 11:59 am WET","ttl":"60","location":{"city":"Ponta Delgada","country":"Portugal","region":""},"units":{"distance":"km","pressure":"mb","speed":"km/h","temperature":"C"},"wind":{"chill":"19","direction":"280","speed":"32.19"},"atmosphere":{"humidity":"94","pressure":"1015.92","rising":"0","visibility":"9.99"},"astronomy":{"sunrise":"7:09 am","sunset":"5:41 pm"},"image":{"title":"Yahoo! Weather","width":"142","height":"18","link":"http://weather.yahoo.com","url":"http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif"},"item":{"title":"Conditions for Ponta Delgada, PT at 11:59 am WET","lat":"37.75","long":"-25.66","link":"http://us.rd.yahoo.com/dailynews/rss/weather/Ponta_Delgada__PT/*http://weather.yahoo.com/forecast/POXX0020_c.html","pubDate":"Tue, 04 Nov 2014 11:59 am WET","condition":{"code":"28","date":"Tue, 04 Nov 2014 11:59 am WET","temp":"19","text":"Mostly Cloudy"},"description":"\n<img src=\"http://l.yimg.com/a/i/us/we/52/28.gif\"/><br />\n<b>Current Conditions:</b><br />\nMostly Cloudy, 19 C<BR />\n<BR /><b>Forecast:</b><BR />\nTue - Cloudy. High: 19 Low: 17<br />\nWed - Partly Cloudy. High: 19 Low: 17<br />\nThu - Showers. High: 19 Low: 14<br />\nFri - Partly Cloudy. High: 17 Low: 16<br />\nSat - PM Showers. High: 18 Low: 15<br />\n<br />\n<a href=\"http://us.rd.yahoo.com/dailynews/rss/weather/Ponta_Delgada__PT/*http://weather.yahoo.com/forecast/POXX0020_c.html\">Full Forecast at Yahoo! Weather</a><BR/><BR/>\n(provided by <a href=\"http://www.weather.com\" >The Weather Channel</a>)<br/>\n","forecast":[{"code":"26","date":"4 Nov 2014","day":"Tue","high":"19","low":"17","text":"Cloudy"},{"code":"30","date":"5 Nov 2014","day":"Wed","high":"19","low":"17","text":"Partly Cloudy"},{"code":"11","date":"6 Nov 2014","day":"Thu","high":"19","low":"14","text":"Showers"},{"code":"30","date":"7 Nov 2014","day":"Fri","high":"17","low":"16","text":"Partly Cloudy"},{"code":"39","date":"8 Nov 2014","day":"Sat","high":"18","low":"15","text":"PM Showers"}],"guid":{"isPermaLink":"false","content":"POXX0020_2014_11_08_7_00_WET"}}}}}}

Depois

$ curl -s "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%3D26189893%20and%20u%3D'c'&format=json" | python -m json.tool
{
    "query": {
        "count": 1,
        "created": "2014-11-04T14:01:01Z",
        "lang": "en-US",
        "results": {
            "channel": {
                "astronomy": {
                    "sunrise": "7:09 am",
                    "sunset": "5:41 pm"
                },
                "atmosphere": {
                    "humidity": "94",
                    "pressure": "1015.92",
                    "rising": "0",
                    "visibility": "9.99"
                },
                "description": "Yahoo! Weather for Ponta Delgada, PT",
                "image": {
                    "height": "18",
                    "link": "http://weather.yahoo.com",
                    "title": "Yahoo! Weather",
                    "url": "http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif",
                    "width": "142"
                },
                "item": {
                    "condition": {
                        "code": "28",
                        "date": "Tue, 04 Nov 2014 11:59 am WET",
                        "temp": "19",
                        "text": "Mostly Cloudy"
                    },
                    "description": "\n<img src=\"http://l.yimg.com/a/i/us/we/52/28.gif\"/><br />\n<b>Current Conditions:</b><br />\nMostly Cloudy, 19 C<BR />\n<BR /><b>Forecast:</b><BR />\nTue - Cloudy. High: 19 Low: 17<br />\nWed - Partly Cloudy. High: 19 Low: 17<br />\nThu - Showers. High: 19 Low: 14<br />\nFri - Partly Cloudy. High: 17 Low: 16<br />\nSat - PM Showers. High: 18 Low: 15<br />\n<br />\n<a href=\"http://us.rd.yahoo.com/dailynews/rss/weather/Ponta_Delgada__PT/*http://weather.yahoo.com/forecast/POXX0020_c.html\">Full Forecast at Yahoo! Weather</a><BR/><BR/>\n(provided by <a href=\"http://www.weather.com\" >The Weather Channel</a>)<br/>\n",
                    "forecast": [
                        {
                            "code": "26",
                            "date": "4 Nov 2014",
                            "day": "Tue",
                            "high": "19",
                            "low": "17",
                            "text": "Cloudy"
                        },
                        {
                            "code": "30",
                            "date": "5 Nov 2014",
                            "day": "Wed",
                            "high": "19",
                            "low": "17",
                            "text": "Partly Cloudy"
                        },
                        {
                            "code": "11",
                            "date": "6 Nov 2014",
                            "day": "Thu",
                            "high": "19",
                            "low": "14",
                            "text": "Showers"
                        },
                        {
                            "code": "30",
                            "date": "7 Nov 2014",
                            "day": "Fri",
                            "high": "17",
                            "low": "16",
                            "text": "Partly Cloudy"
                        },
                        {
                            "code": "39",
                            "date": "8 Nov 2014",
                            "day": "Sat",
                            "high": "18",
                            "low": "15",
                            "text": "PM Showers"
                        }
                    ],
                    "guid": {
                        "content": "POXX0020_2014_11_08_7_00_WET",
                        "isPermaLink": "false"
                    },
                    "lat": "37.75",
                    "link": "http://us.rd.yahoo.com/dailynews/rss/weather/Ponta_Delgada__PT/*http://weather.yahoo.com/forecast/POXX0020_c.html",
                    "long": "-25.66",
                    "pubDate": "Tue, 04 Nov 2014 11:59 am WET",
                    "title": "Conditions for Ponta Delgada, PT at 11:59 am WET"
                },
                "language": "en-us",
                "lastBuildDate": "Tue, 04 Nov 2014 11:59 am WET",
                "link": "http://us.rd.yahoo.com/dailynews/rss/weather/Ponta_Delgada__PT/*http://weather.yahoo.com/forecast/POXX0020_c.html",
                "location": {
                    "city": "Ponta Delgada",
                    "country": "Portugal",
                    "region": ""
                },
                "title": "Yahoo! Weather - Ponta Delgada, PT",
                "ttl": "60",
                "units": {
                    "distance": "km",
                    "pressure": "mb",
                    "speed": "km/h",
                    "temperature": "C"
                },
                "wind": {
                    "chill": "19",
                    "direction": "280",
                    "speed": "32.19"
                }
            }
        }
    }
}

Neat and tidy :)

Comentários