• Stars
    star
    107
  • Rank 323,587 (Top 7 %)
  • Language
    Shell
  • License
    MIT License
  • Created over 11 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Load, Dump and Manipulate JSON in Bash

Name

JSON - JSON for Bash

json-bash

Synopsis

source json.bash

json='{"name":"Jason","friends":["Jimmy","Joe"]}'
JSON.load "$json"
joe=$(JSON.get /friends/1)
JSON.put /friends/2 Jeff
new_json=$(JSON.dump)

Description

The json.bash library provides functions for loading, manipulating and dumping JSON data.

Bash doesn't have nestable hashes and arrays to load JSON data into. This library provides a different data model called a "linear tree". A linear tree is simply a sequence of text lines, each containing a key-path and a leaf- value (separated by a tab):

/key/path<HARD-TAB>"leaf value"

Thus this JSON:

{
  "name": {
    "first": "Jimmy",
    "last": "James"
  },
  "list": [
    "A string",
    42,
    [ true, false, null ]
  ]
}

Would be loaded as this linear tree:

/name/first     "Jimmy"
/name/last      "James"
/list/0 "A string"
/list/1 42
/list/2/0       true
/list/2/1       false
/list/2/2       null

Since Bash has many tools for searching and manipulating text, the linear form works fairly well for common JSON data operations.

Installation

Just run:

make install

This may require sudo permission.

By default, this will install:

/usr/local/lib/bash/test-simple.bash
/usr/local/share/man/man1/test-simple.1

You can change the install locations with these environment variables:

  • PREFIX - default is /usr/local

  • INSTALL_LIB

  • INSTALL_MAN

Run make help to see all the make targets that are available.

API Functions

The library contains functions to load JSON to the linear form and to dump that form back into JSON. It also has functions to retrieve, modify, add and remove data from the linear form.

JSON.load [ <json-string> [<linear-var-name>]]

This function takes JSON as input and generates a linear tree as output.

With no arguments, input is read from stdin and output is written to stdout. With one argument, the input is provided as a string argument and the output is stored in an internal cache variable. With two arguments the input is again provided as a string argument, and the output is copied into the variable name provided.

JSON.dump [<linear-var-name>]

This function takes a linear tree as input and generates JSON as output.

With no arguments, input is read from stdin. With one argument, input is taken from the provided variable name. To use the internal cache, use - as the variable name. Output is always written to stdout.

JSON.get [-a|-s|-b|-n|-z] <key-path> [<linear-var-name>]

This function takes a key path and returns the corresponding value. If the key is found, the exit status is 0, otherwise it is 1. If the value is a string, it will be enclosed in double quotes. Otherwise it will be a number or the unquoted strings: true, false or null.

With just the one required argument, the linear tree will be obtained from stdin. Otherwise it can be provided with a variable name (or - for the cache). The value (if any) is written to stdout.

See FLAGS below for an explanation of the flag options.

JSON.keys <key-path> [<linear-var-name>]

This function takes a key path and returns the keys of the corresponding object, one per line. If the key is not found or the value of the key is not an object, it will return nothing.

JSON.object <key-path> [<linear-var-name>]

This function takes a key path and returns the corresponding object. If the key is not found, it will return nothing.

JSON.put [-s|-b|-n|-z] <key-path> <new-value> [<linear-var-name>]

This function adds a path/value pair to the linear tree. If the path already exists, the value will be replaced, otherwise it will be added.

With just the two required arguments, the linear tree will be obtained from stdin. Otherwise it can be provided with a variable name (or - for the cache). Nothing will be written to stdout and the exit status will always be 0.

JSON.del <key-path> <new-value> [<linear-var-name>]

This function removes a path/value pair from the linear tree, if it exists.

With just the one required argument, the linear tree will be obtained from stdin. Otherwise it can be provided with a variable name (or - for the cache). Nothing will be written to stdout and the exit status will always be 0.

Heuristics will be used to determine what type the value is. The -s flag indicates the value is a string, the -n flag indicates a number,the -b flag indicates a boolean and the -z flag indicates a null value.

JSON.cache [<linear-var-name>]

Outputs the value of the internal linear tree cache string.

With no arguments, the value is written to stdout. With one argument, the value is copied to the variable name provided.

Flags

The command flags -a, -s, -n, -b and -z indicate the type of value provided or expected, and they refer to Any, String, Number, Boolean and Null respectively. If the type of data doesn't look like the type indicated by the flag, the command will return with a status code of 2.

If -a is used, the double quotes at each end (if any) will be removed for a JSON.get.

If -s is used and the data is a string, the double quotes will be removed for a JSON.get or added for a JSON.put.

The -n flag requires no value transformation, but it will cause the command to fail (status 2) if the value is not a number.

If -b is used for a JSON.get, true will become 0 and false will become 1. JSON.put will do the reverse. This follows Bash's idea of using 0 for a successful return code.

The -z flag on a JSON.get will turn null into the empty string, and for a JSON.put will turn any value into null.

Examples

# Load JSON to linear tree
JSON.load "$(< file.json)" tree
# or:
tree=$(cat file.json | JSON.load)

# Get a value
first_name=$(JSON.get /name/first tree)
# or:
first_name=$(echo "$tree" | JSON.get /name/first)

# Change a value
JSON.put /name/first Jimmy tree
# or:
tree=(echo "$tree" | JSON.put /name/first Jimmy)

# Delete a value
JSON.del /name/middle tree
# or:
tree=(echo "$tree" | JSON.del /name/middle)

# Dump a linear tree to JSON
JSON.dump tree > new-file.json
# or:
echo "$tree" | JSON.dump > new-file.json

Problems

This library is meant to be useful for solving common problems involving JSON. However, without the native JSON object model in Bash, it becomes problematic the further you stray from the norm.

Here is a list of known issues. Some may be addressed, some are very likely to stay out of scope:

  • No support for empty arrays and empty objects.

  • No support for key-paths that refer to objects or arrays.

  • No support for common array operations like push, pop, splice, etc.

Todo

  • Support object keys that:

    • Contain whitespace

    • Consist of all digit characters

    • Contain backslashes

  • Implement JSON.dump

Status

Please report any issues to https://github.com/ingydotnet/json-bash/issues

Or find me on [email protected].

Author

Written by Ingy dΓΆt Net <[email protected]>

Copyright & License

Copyright 2013-2016 Ingy dΓΆt Net

The MIT License (MIT)

More Repositories

1

git-subrepo

Shell
3,199
star
2

git-hub

Do GitHub operations from the `git` command
Shell
794
star
3

vroom-pm

Vim Based Slideshow Presentations
Perl
110
star
4

...

Dot Dot Dot
Perl
109
star
5

jemplate

Industrial strength JavaScript template framework
Perl
64
star
6

yaml-vim

YAML Highlight script for VIM editor
Vim Script
50
star
7

mo-pm

Perl Micro Objects
Perl
41
star
8

io-all-pm

All in One Perl IO
Perl
38
star
9

bashplus

Modern Bash Programming Framework
Shell
36
star
10

yaml-libyaml-pm

Perl Binding to libyaml
C
33
star
11

pairup

Shell
29
star
12

yaml-pm

YAML Perl Module
Perl
20
star
13

dotdotdot

Organize and share your *nix dot files
Vim Script
19
star
14

pquery-pm

Perl Port of jQuery
Perl
19
star
15

cdent-py

C'Dent - Portable Module Programming Language
Python
19
star
16

inline-pm

Write Perl subroutines in other programming languages
Perl
19
star
17

xxx-pm

See Your Data in the Nude
Makefile
16
star
18

swim-pm

Perl
15
star
19

test-more-bash

Shell
15
star
20

test-base-pm

Extendable Perl Testing
Perl
13
star
21

rosettacode-pm

RosettaCode Data Extractor
Perl
12
star
22

lola

Shell
12
star
23

yaml-perl-pm

Pure Perl Port of PyYAML
Python
11
star
24

ajaxterm

Fork of antony.lesuisse.org/ajaxterm
Python
10
star
25

inline-c-pm

Perl
10
star
26

mousse-pm

A Light and Tasty Moose for CPAN Authors
Perl
10
star
27

testml-pm6

TestML for Perl 6
Perl 6
10
star
28

testml-pm

TestML for Perl
Perl
9
star
29

testml

TestML Specification and Documentation
9
star
30

git-xs-pm

Perl XS Wrapper of libgit2
C
8
star
31

package-py

Common parts for Python packages
Python
8
star
32

scim-query-filter-parser-rb

Ruby
8
star
33

stump-pm

Larry Wall's Stump Speech Slideshow Software
Perl
7
star
34

test-base-js

Test.Base module for JavaScript
7
star
35

boolean-pm

Boolean Type Support for Perl
Perl
7
star
36

live-demo

Shell
7
star
37

this-shit

Fork this-shit
7
star
38

testml-tml

TestML Tests for TestML
6
star
39

path-manip-sh

Bash PATH manipulation functions
Shell
6
star
40

yaml-book-2010

YAML - The Book
Ruby
6
star
41

pairup-stackato

PairUp! - The Pair Programming Station that runs as a Stackato App
Shell
6
star
42

only-pm

Load specific Perl module versions
Perl
6
star
43

project-site

Static Content Website Generator
Shell
6
star
44

template-toolkit-simple-pm

A Simple Interface to Template Toolkit
Perl
6
star
45

class-js

Extremely lightweight, but useful JavaScript class wrapper
Perl
6
star
46

yaml-shell-pm

A YAML/Perl Interactive Shell
Perl
6
star
47

jsony-pm

JSONY - Relaxed JSON with a little bit of YAML
Raku
6
star
48

pst

Shell
5
star
49

yaml3-pm

YAML Implementation for Perl 5 using Pegex
Perl
5
star
50

ingydotnet-resume

Ingy dot Net's resume
HTML
5
star
51

perfect-demo

Shell
5
star
52

perl5-pm

Framework module for bundling Perl5 modules
Perl
5
star
53

crockford-py

Crockford base32 encode module for Python
Python
5
star
54

app-aycabtu-pm

All Your Codez Are Belong To Us
Perl
5
star
55

zilla-dist-pm

Perl
5
star
56

ingy-dots

Personal Dot Files
Shell
5
star
57

stardoc-pm

Acmeist Documentation
Perl
5
star
58

cog-osdctw2011-talk

Cog Talk for OSDC::TW 2011
5
star
59

yaml-js

YAML for JavaScript
4
star
60

acmeism-osdctw2010-talk

Please Check Your Guns at the Door -- C'Dent, the Acmeism and Everyone
4
star
61

kwiki

Kwiki Wiki Framework
Perl
4
star
62

yamltime-pm

YAML based Time Tracker App
Perl
4
star
63

yadda

Perl
4
star
64

plack-middleware-cache-pm

Response Caching Middleware for Perl's Plack
Perl
4
star
65

file-share-pm

Improved File::ShareDir for Perl
Makefile
4
star
66

lexicals-pm

Create a hash of your 'my' variables
Perl
4
star
67

moos-pm

Moo Simple
Perl
4
star
68

wikiwyg-net-site

Wikiwyg Website - Static Content
Smalltalk
4
star
69

boot-dots

... bootstrapping dot files
Shell
4
star
70

language-snusp-pm

SNUSP Programming Language Interpreter
Makefile
4
star
71

wikitext-pm

WikiText.pm
Perl
3
star
72

testml-pgx

TestML Spec Grammar in YAML and JSON
JavaScript
3
star
73

testml-py

TestML for Python
Python
3
star
74

jsync-spec

JSYNC Specification
3
star
75

pegex-crontab-pm

Pegex Crontab Parser
Perl
3
star
76

devel-local-pm

Use development versions of other modules
Perl
3
star
77

spiffy-pm

Spiffy Perl Interface Framework For You
Perl
3
star
78

pyplay-py

Play Around in Python
Python
3
star
79

projects-ingy-net-site

Ingy's Project Site -- Static Content
Smalltalk
3
star
80

pegex-pegex-emitter-perl6regex-pm

Pegex Pegex Emitter for Perl 6 Regexes
Perl
3
star
81

yaml-pgx

Pegex Grammar for YAML
Makefile
3
star
82

yaml-to-json-docker

JavaScript
3
star
83

jsync-pm

JSYNC Module for Perl
Perl
3
star
84

module-manifest-skip-pm

MANIFEST.SKIP Management for Perl
Perl
3
star
85

alt-pm

CPAN Module Documenting the Alt Namespace
Perl
3
star
86

pegex-forth-pm

Perl
3
star
87

www.jemplate.net

JavaScript
3
star
88

xxx-pm6

See Your Perl 6 Data in the Nude
Perl
3
star
89

gloom-pm

Great Little OO Module
Perl
3
star
90

testml-code

TestML Examples
Perl
3
star
91

bpan-bash

Shell
3
star
92

yaml-dev-kit

Perl
3
star
93

test-tap-bash

Shell
3
star
94

pyyaml-mirror

A git mirror of PyYAML
Python
3
star
95

test-more-bash-pm

Shell
3
star
96

libyaml-mirror

A git mirror of libyaml
C
3
star
97

yes

CoffeeScript
2
star
98

acmeism-yapceu-2011-talk

YAPC Europe 2011 Acmeism Talk
2
star
99

pegex-ppw2010-talk

Pegex Talk at Pittsburgh Perl Workshop 2010
2
star
100

linux-setup-sh

Bash setup scripts for new Linux installs
Shell
2
star