Archive

Posts Tagged ‘json’

Google’s URL shortener

June 25, 2013 Leave a comment

Problem
You want to shorten a long URL from the command line / from a script.

Solution
There are lots of URL shorteners. With the Google URL shortener you can do it like this:

curl https://www.googleapis.com/urlshortener/v1/url -H 'Content-Type: application/json' -d '{"longUrl": "https://ubuntuincident.wordpress.com"}'

Sample output:

{
    "kind": "urlshortener#url",
    "id": "http://goo.gl/Zeigx",
    "longUrl": "https://ubuntuincident.wordpress.com/"
}

Exercise
Let’s do it in Python using the requests module:

import requests
import json

url = "https://www.googleapis.com/urlshortener/v1/url"
data = {"longUrl": "https://ubuntuincident.wordpress.com"}
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.post(url, data=json.dumps(data), headers=headers)
print r.text
print 'Short URL:', r.json()["id"]

Links

jq — a lightweight and flexible command-line JSON processor

October 21, 2012 Leave a comment

jq is like sed for JSON data – you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text.

jq is written in portable C, and it has zero runtime dependencies.

jq can mangle the data format that you have into the one that you want with very little effort…” (link)

Check out the tutorial here.

You can also use jq to pretty print an ugly JSON file:

cat ugly_one_liner.json | jq '.'
Categories: bash Tags: , , ,

GitHub: contact watchers

September 7, 2011 Leave a comment

Problem
You want to contact all the watchers of a project. For instance, you want to notify them about some radical changes.

Solution
Simply click on the “Eye” icon that shows the number of watchers. It will list your followers.

Or, you can get the list of watchers through an API:

curl http://github.com/api/v2/json/repos/show/USERNAME/REPONAME/watchers?full=1 | python -mjson.tool

More info about the Repositories API: here. General information about the APIs: here.

Categories: Uncategorized Tags: , , , ,

Pretty print a JSON file

August 10, 2011 Leave a comment

This post is based on the following SO threads: one; two.

Problem

You have an unreadable JSON file from which you want to extract some data… How to prettify it, i.e. how to make it human readable?

Solution

There are web-based and command-line solutions. As an extra, we show you how to do it in Vim too.

Web-based prettifiers

Command-line beautifiers

Vim :)

This tip is based on this post: Editing json files in vim.

In my .vimrc file I had to add the following lines:

" pretty-print JSON files
autocmd BufRead,BufNewFile *.json set filetype=json
" json.vim is here: http://www.vim.org/scripts/script.php?script_id=1945
autocmd Syntax json sou ~/.vim/syntax/json.vim
" json_reformat is part of yajl: http://lloyd.github.com/yajl/
autocmd FileType json set equalprg=json_reformat

When opening a .json file, it will be colored using the json.vim syntax file. Selecting a text and pressing the “=” button will indent the marked text using json_reformat.

Firefox add-on

There are several JSON visualizer add-ons for Firefox, e.g. JSONView.

Follow

Get every new post delivered to your Inbox.

Join 61 other followers