Edit page in Livemark
(2024-11-22 08:02)

Json Format

Frictionless supports parsing JSON tables (JSON and JSONL/NDJSON).

pip install frictionless[json]
pip install 'frictionless[json]' # for zsh shell

Reading Data

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': '中国人'}]

Writing Data

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'  |
+----+-----------+

Configuration

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': '中国人'}]

Reference

formats.JsonControl (class)

formats.JsonControl (class)

Json control representation. Control class to set params for JSON reader/writer class.

Signature

(*, 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

Parameters

  • name (Optional[str])
  • title (Optional[str])
  • description (Optional[str])
  • keys (Optional[List[str]])
  • keyed (bool)
  • property (Optional[str])

formats.jsonControl.keys (property)

Specifies the keys/columns to read from the resource. For example: keys=["id","name"].

Signature

Optional[List[str]]

formats.jsonControl.keyed (property)

If set to True, It returns the data as key:value pair. Default value is False.

Signature

bool

formats.jsonControl.property (property)

This property specifies the path to the attribute in a json file, it it has nested fields.

Signature

Optional[str]