Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
oh-my-zsh
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
github
oh-my-zsh
Commits
7ded6752
Commit
7ded6752
authored
Sep 10, 2021
by
Eduardo Flores
Committed by
Marc Cornellà
Sep 22, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(jsontools): add tools to pretty print json-lines (ndjson) (#10176)
Co-authored-by:
Marc Cornellà
<
hello@mcornella.com
>
parent
b5909396
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
9 deletions
+48
-9
README.md
plugins/jsontools/README.md
+39
-8
jsontools.plugin.zsh
plugins/jsontools/jsontools.plugin.zsh
+9
-1
No files found.
plugins/jsontools/README.md
View file @
7ded6752
...
@@ -17,32 +17,63 @@ Usage is simple... just take your json data and pipe it into the appropriate jso
...
@@ -17,32 +17,63 @@ Usage is simple... just take your json data and pipe it into the appropriate jso
-
`urlencode_json`
: returns a url encoded string for the given json.
-
`urlencode_json`
: returns a url encoded string for the given json.
-
`urldecode_json`
: returns decoded json for the given url encoded string.
-
`urldecode_json`
: returns decoded json for the given url encoded string.
### Supports NDJSON (Newline Delimited JSON)
The plugin also supports
[
NDJSON
](
http://ndjson.org/
)
input, which means all functions
have an alternative function that reads and processes the input line by line. These
functions have the same name except using
`ndjson`
instead of
`json`
:
> `pp_ndjson`, `is_ndjson`, `urlencode_ndjson`, `urldecode_ndjson`.
### Examples
### Examples
-
**pp_json**
:
-
**pp_json**
:
```
sh
```
console
#
curl json data and pretty print the results
#
curl json data and pretty print the results
curl https://coderwall.com/bobwilliams.json | pp_json
curl https://coderwall.com/bobwilliams.json | pp_json
```
```
-
**is_json**
:
-
**is_json**
:
```
sh
```
console
# Validate if file's content conforms to a valid JSON schema
#
validate
if
file
's content conforms to a valid JSON schema
less data.json | is_json
$
is_json < data.json
true
#
shows
true
/
false
and returns the proper
exit
code
$
echo
$?
0
```
```
-
**urlencode_json**
:
-
**urlencode_json**
:
```
sh
```
console
#
json data directly from the
command
line
#
json data directly from the
command
line
echo
'{"b":2, "a":1}'
| urlencode_json
$
echo
'{"b":2, "a":1}'
| urlencode_json
%7B%22b%22:2,%20%22a%22:1%7D
```
```
-
**urldecode_json**
:
-
**urldecode_json**
:
```
sh
```
console
#
url encoded string to decode
#
url encoded string to decode
echo
'%7B%22b%22:2,%20%22a%22:1%7D%0A'
| urldecode_json
$
echo
'%7B%22b%22:2,%20%22a%22:1%7D'
| urldecode_json
{"b":2, "a":1}
```
-
**pp_ndjson**
:
```
console
#
echo
two separate json objects and pretty print both
$
echo
'{"a": "b"}\n{"c": [1,2,3]}'
| pp_ndjson
{
"a": "b"
}
{
"c": [
1,
2,
3
]
}
```
```
plugins/jsontools/jsontools.plugin.zsh
View file @
7ded6752
...
@@ -101,5 +101,13 @@ print(unquote_plus(sys.stdin.read()))
...
@@ -101,5 +101,13 @@ print(unquote_plus(sys.stdin.read()))
}
}
;;
;;
esac
esac
unset
JSONTOOLS_METHOD
unset
JSONTOOLS_METHOD
## Add NDJSON support
function
{
pp,is,urlencode,urldecode
}
_ndjson
()
{
local
json
jsonfunc
=
"
${
0
//ndjson/json
}
"
while
read
-r
json
;
do
$jsonfunc
<<<
"
$json
"
done
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment