Frictionless supports parsing JSON tables (JSON and JSONL/NDJSON).
pip install frictionless[json]
pip install 'frictionless[json]' # for zsh shell
We use the
path
argument to ensure that it will not be guessed to be a metadata file
You can read this format using Package/Resource
, for example:
from pprint import pprint
from frictionless import resources
resource = resources.TableResource(path='table.json')
pprint(resource.read_rows())
[{'id': 1, 'name': 'english'}, {'id': 2, 'name': '中国人'}]
The same is actual for writing:
from frictionless import Resource, resources
source = Resource(data=[['id', 'name'], [1, 'english'], [2, 'german']])
target = resources.TableResource(path='table-output.json')
source.write(target)
print(target)
print(target.to_view())
{'name': 'table-output',
'type': 'table',
'path': 'table-output.json',
'scheme': 'file',
'format': 'json',
'mediatype': 'text/json'}
+----+-----------+
| id | name |
+====+===========+
| 1 | 'english' |
+----+-----------+
| 2 | 'german' |
+----+-----------+
There is a dialect to configure how Frictionless read and write files in this format. For example:
from pprint import pprint
from frictionless import Resource, resources, formats
control=formats.JsonControl(keyed=True)
resource = resources.TableResource(path='table.keyed.json', control=control)
pprint(resource.read_rows())
[{'id': 1, 'name': 'english'}, {'id': 2, 'name': '中国人'}]
Json control representation. Control class to set params for JSON reader/writer class.
(*, name: Optional[str] = None, title: Optional[str] = None, description: Optional[str] = None, keys: Optional[List[str]] = None, keyed: bool = False, property: Optional[str] = None) -> None
Specifies the keys/columns to read from the resource. For example: keys=["id","name"].
Optional[List[str]]
If set to True, It returns the data as key:value pair. Default value is False.
bool
This property specifies the path to the attribute in a json file, it it has nested fields.
Optional[str]