Skip to content
Permalink
Browse files

first commit

  • Loading branch information...
tidwall committed Oct 19, 2016
0 parents commit 8444861795d2e611be977891320f23e9822ba789
22 LICENSE
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2016 Josh Baker

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


@@ -0,0 +1,10 @@
all:
@./build.sh
clean:
@rm -f jsoned
install: all
@cp jsoned /usr/local/bin
uninstall:
@rm -f /usr/local/bin/jsoned
package:
@NOCOPY=1 ./build.sh package
150 README.md
@@ -0,0 +1,150 @@
<p align="center">
<img
src="logo.png"
width="240" height="78" border="0" alt="JSONed">
<br>
JSON Stream Editor
</p>

jsoned is a command line utility that provides a fast and simple way to retrieve or update values from JSON documents.
It uses [GJSON](https://github.com/tidwall/gjson) and [SJSON](https://github.com/tidwall/sjson) under the hood.

It's fast because it avoids parsing irrelevant sections of json, skipping over values that do not apply, and aborts as soon as the target value has been found or updated.

Getting started
---------------

### Building

jsoned can be compiled and used on Linux, OSX, Windows, FreeBSD, and probably others since the codebase is 100% Go.
We support both 32 bit and 64 bit systems. Go must be installed on the build machine.

To build simply:
```
$ make
```

Or [download a pre-built binary](https://github.com/tidwall/jsoned/releases) for Linux, OSX, Windows, or FreeBSD.


Usage menu:
```
$ jsoned -h
usage: jsoned [-v value] [-s] [-i infile] [-o outfile] keypath
examples: jsoned keypath read value from stdin
or: jsoned -i infile keypath read value from infile
or: jsoned -v value keypath edit value
or: jsoned -v value -o outfile keypath edit value and write to outfile
options:
-v value Edit JSON key path value
-i infile Use input file instead of stdin
-o outfile Use output file instead of stdout
-r Use raw values, otherwise types are auto-detected
keypath JSON key path (like "name.last")
```


Examples
--------

### Getting a value

jsoned uses a special [path syntax](https://github.com/tidwall/gjson#path-syntax) for finding values.

Get a string:
```sh
$ echo '{"name":{"first":"Tom","last":"Smith"}}' | jsoned name.last
Smith
```

Get a block of JSON:
```sh
$ echo '{"name":{"first":"Tom","last":"Smith"}}' | jsoned name
{"first":"Tom","last":"Smith"}
```

Try to get a non-existent key:
```sh
$ echo '{"name":{"first":"Tom","last":"Smith"}}' | jsoned name.middle
null
```

Get the raw string value:
```sh
$ echo '{"name":{"first":"Tom","last":"Smith"}}' | jsoned -r name.last
"Smith"
```

Get an array value by index:
```sh
$ echo '{"friends":["Tom","Jane","Carol"]}' | jsoned friends.1
Jane
```

### Setting a value

The [path syntax](https://github.com/tidwall/sjson#path-syntax) for setting values has a couple of tiny differences than for getting values.

The `-v value` option is auto-detected as a Number, Boolean, Null, or String.
You can override the auto-detection and input raw JSON by including the `-r` option.
This is useful for raw JSON blocks such as object, arrays, or premarshalled strings.

Update a value:
```sh
$ echo '{"name":{"first":"Tom","last":"Smith"}}' | jsoned -v Andy name.first
{"name":{"first":"Andy","last":"Smith"}}
```

Set a new value:
```sh
$ echo '{"name":{"first":"Tom","last":"Smith"}}' | jsoned -v 46 age
{"age":46,"name":{"first":"Andy","last":"Smith"}}
```

Set a new nested value:
```sh
$ echo '{"name":{"first":"Tom","last":"Smith"}}' | jsoned -v relax task.today
{"task":{"today":"relax"},"name":{"first":"Andy","last":"Smith"}}
```

Replace an array value by index:
```sh
$ echo '{"friends":["Tom","Jane","Carol"]}' | jsoned -v Andy friends.1
{"friends":["Tom","Andy","Carol"]}
```

Append an array:
```sh
$ echo '{"friends":["Tom","Jane","Carol"]}' | jsoned -v Andy friends.-1
{"friends":["Tom","Andy","Carol","Andy"]}
```

Set an array value that's past the bounds:
```sh
$ echo '{"friends":["Tom","Jane","Carol"]}' | jsoned -v Andy friends.5
{"friends":["Tom","Andy","Carol",null,null,"Andy"]}
```

Set a raw block of JSON:
```sh
$ echo '{"name":"Carol"}' | jsoned -r -v '["Tom","Andy"]' friends
{"friends":["Tom","Andy"],"name":"Carol"}
```

Start new JSON document:
```sh
$ echo '' | jsoned -v 'Sam' name.first
{"name":{"first":"Sam"}}
```

## Contact
Josh Baker [@tidwall](http://twitter.com/tidwall)

## License
jsoned source code is available under the MIT [License](/LICENSE).



@@ -0,0 +1,66 @@
#!/bin/bash
set -e

VERSION="0.1.0"
PROTECTED_MODE="no"

export GO15VENDOREXPERIMENT=1

cd $(dirname "${BASH_SOURCE[0]}")
OD="$(pwd)"
WD=$OD

# temp directory for storing isolated environment.
TMP="$(mktemp -d -t sdb.XXXX)"
function rmtemp {
rm -rf "$TMP"
}
trap rmtemp EXIT

if [ "$NOCOPY" != "1" ]; then
# copy all files to an isloated directory.
WD="$TMP/src/github.com/tidwall/jsoned"
export GOPATH="$TMP"
for file in `find . -type f`; do
# TODO: use .gitignore to ignore, or possibly just use git to determine the file list.
if [[ "$file" != "." && "$file" != ./.git* && "$file" != ./jsoned ]]; then
mkdir -p "$WD/$(dirname "${file}")"
cp -P "$file" "$WD/$(dirname "${file}")"
fi
done
cd $WD
fi

package(){
echo Packaging $1 Binary
bdir=jsoned-${VERSION}-$2-$3
rm -rf packages/$bdir && mkdir -p packages/$bdir
GOOS=$2 GOARCH=$3 ./build.sh
if [ "$2" == "windows" ]; then
mv jsoned packages/$bdir/jsoned.exe
else
mv jsoned packages/$bdir
fi
cp README.md packages/$bdir
cd packages
if [ "$2" == "linux" ]; then
tar -zcf $bdir.tar.gz $bdir
else
zip -r -q $bdir.zip $bdir
fi
rm -rf $bdir
cd ..
}

if [ "$1" == "package" ]; then
rm -rf packages/
package "Windows" "windows" "amd64"
package "Mac" "darwin" "amd64"
package "Linux" "linux" "amd64"
package "FreeBSD" "freebsd" "amd64"
exit
fi

# build and store objects into original directory.
go build -ldflags "-X main.version=$VERSION" -o "$OD/jsoned" cmd/jsoned/*.go

0 comments on commit 8444861

Please sign in to comment.
You can’t perform that action at this time.