Some useful utility scripts.
A small query language for quick analyses of json files.
Suppose you have a file called "example.json":
{
"menu": {
"id": "file",
"popup": {
"menuitem": [
{
"onclick": "CreateNewDoc()",
"value": "New"
},
{
"onclick": "OpenDoc()",
"value": "Open"
},
{
"onclick": "CloseDoc()",
"value": "Close"
}
]
},
"value": "File"
}
}
Then you can access members like this:
# print whole file:
$ jsonql.py example.json
# print value of menu.id:
$ jsonql.py example.json menu.id
# print second menuitem:
$ jsonql.py example.json menu.popup.menuitem[1]
A method to convert a list of dictionaries to a list of lists in a csv style.
listOfDicts = [
{
"onclick": "CreateNewDoc()",
"value": "New"
},
{
"onclick": "OpenDoc()",
"value": "Open"
},
{
"onclick": "CloseDoc()",
"value": "Close"
}
]
listOfLists = flattenDict(listOfDicts, includeHeaders=True)
Value of listOfLists:
[
['onclick', 'value'], #headers
['CreateNewDoc()', 'New'],
['OpenDoc()', 'Open'],
['CloseDoc()', 'Close']
]
Suppose you have a file "listOfDicts.json" which contains the listOfDicts structure from the example above.
$ flattenDict.py listOfDicts.json
"onclick","value"
"CreateNewDoc()","New"
"OpenDoc()","Open"
"CloseDoc()","Close"