- python 3
pip3 install jpio
Json Python IO or JPIO is a simple Json reader/writer written in python that can be used in the command line. It uses JsTQL, a simple Json Transformation and Query Language that is created just for this purpose.
JsTQL can be imported in any python program and it contains a parser and a runtime. JPIO is simply a wrapper around it.
{
"version" : {
"major" : 1,
"minor" : 0,
"patch" : 0
},
"books" : [
{
"name" : "Introduction to Json",
"isbn" : "M19165029",
"author" : "1"
},
{
"name" : "Introduction to Python",
"isbn" : "M35123115",
"author" : "2"
},
{
"name" : "Crazy JPIO",
"isbn" : "M51236131",
"author" : "3"
}
],
"authors": [
{
"id": 1,
"name" : "That guy that made Json"
},
{
"id": 2,
"name" : "That guy that created Python"
},
{
"id": 3,
"name" : "ZwodahS"
}
]
}
$ cat sample/books.json | jpio '.version'
output:
{"patch": 0, "minor": 0, "major": 1}
$ cat sample/books.json | jpio '.version.major'
output:
1
$ cat sample/books.json | jpio '.books'
[{"isbn": "M19165029", "author": "1", "name": "Introduction to Json"}, {"isbn": "M35123115", "author": "2", "name": "Introduction to Python"}, {"isbn": "M51236131", "author": "3", "name": "Crazy JPIO"}]
$ cat sample/books.json | jpio '.books.[:2]'
[{"author": "1", "name": "Introduction to Json", "isbn": "M19165029"}, {"author": "2", "name": "Introduction to Python", "isbn": "M35123115"}]
$ cat sample/books.json | jpio '.books.[*].author'
["1", "2", "3"]
$ cat sample/books.json | jpio -s '.books.[*].author'
1
2
3
$ cat sample/books.json | jpio '.books.[*].price=30'
output:
{
"books": [
{"author": "1", "price": 30, "name": "Introduction to Json", "isbn": "M19165029"},
{"author": "2", "price": 30, "name": "Introduction to Python", "isbn": "M35123115"},
{"author": "3", "price": 30, "name": "Crazy JPIO", "isbn": "M51236131"}
],
"version": {"minor": 0, "patch": 0, "major": 1},
"authors": [{"name": "That guy that made Json"}, {"name": "That guy that created Python"}, {"name": "ZwodahS"}]
}
$ cat sample/books.json | jpio '.books.[1].price=30'
{
"version": { "major": 1, "patch": 0, "minor": 0 },
"authors": [
{ "name": "That guy that made Json" },
{ "name": "That guy that created Python" },
{ "name": "ZwodahS" }
],
"books": [
{ "author": "1", "name": "Introduction to Json", "isbn": "M19165029" },
{ "author": "2", "name": "Introduction to Python", "price": 30, "isbn": "M35123115" },
{ "author": "3", "name": "Crazy JPIO", "isbn": "M51236131" }
]
}
$ echo '{}' | jpio '.version=j({"major":1, "minor":0, "patch":0})'
output:
{"version": {"patch": 0, "major": 1, "minor": 0}}
$ echo '{}' | jpio '.values=j([5,4,3,2,1])'
output:
{"values": [5, 4, 3, 2, 1]}
$ echo '{"values": [5, 4, 3, 2, 1]}' | jpio '.values#sort()'
output:
{"values": [1, 2, 3, 4, 5]}
$ echo '{"values": [ {"a":3}, {"a":5}, {"a":1} ]}' | jpio '.values#sort(a)'
output:
{"values": [{"a": 1}, {"a": 3}, {"a": 5}]}
$ echo '{ "values": { "a": 1, "b": 2, "c": 3} }' | jpio '.values#keys()'
or
$ echo '{ "a": 1, "b": 2, "c": 3}' | jpio '#keys()' # if object is at root level
$ cat sample/books.json | jpio '.books.[*].author=(.$root.authors.[(.author)])'
This should denormalize the data and store the author dict in the books.
The ability to construct strings from various data.
$ cat sample/books.json | jpio '.books#find(author=2).price=30'
Coming soon ...