File: EXAMPLES.rst

package info (click to toggle)
jsonpipe 0.0.8-4
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 176 kB
  • sloc: python: 681; sh: 17; makefile: 4
file content (69 lines) | stat: -rw-r--r-- 2,022 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
========
Examples
========

This file enumerates some common patterns for working with jsonpipe output. The
examples here use the file ``example.json``, which can be found in the root of
this repo. Basic familiarity with the UNIX shell, common utilities and regular
expressions is assumed.


Simple Selection
================

In Python::

    >>> json[12]['user']['screen_name']
    u"ManiacasCarlos"

On the command-line, just grep for the specific path you're looking for::

    $ jsonpipe < example.json | grep -P '^/12/user/screen_name\t'
    /12/user/screen_name	"ManiacasCarlos"

The pattern used here is terminated with ``\t``, because otherwise we'd get
sub-components of the ``screen_name`` path if it were an object, and we'd also
pick up any keys which started with the string ``screen_name`` (so we might get
``screen_name_123`` and ``screen_name_abc`` if those keys existed).


Extracting Entire Objects
=========================

In Python::

    >>> json[12]['user']
    {... u'screen_name': u'ManiacasCarlos', ...}

On the command-line, grep for the path again but terminate with ``/`` instead
of ``\t``::

    $ jsonpipe < example.json | grep -P '^/12/user/'
    ...
    /12/user/profile_use_background_image	true
    /12/user/protected	false
    /12/user/screen_name	"ManiacasCarlos"
    /12/user/show_all_inline_media	false
    ...

You can also filter for either a simple value *or* an entire object by
terminating the pattern with a character range::

    $ jsonpipe < example.json | grep -P '^/12/user[/\t]'
    /12/user	{}
    /12/user/contributors_enabled	false
    /12/user/created_at	"Mon Jan 31 20:42:31 +0000 2011"
    /12/user/default_profile	false
    ...


Searching Based on Equality or Patterns
=======================================

Find users with a screen name beginning with a lowercase 'm'::

    $ jsonpipe < example.json | grep -P '/user/screen_name\t"m'
    /0/user/screen_name	"milenemagnus_"
    /11/user/screen_name	"mommiepreneur"
    /14/user/screen_name	"mantegavoadora"