Skip to main content

Map flat data to structured JSON via a mapping.

Project description

# jsonmapping [![Build Status](https://travis-ci.org/pudo/jsonmapping.svg?branch=master)](https://travis-ci.org/pudo/jsonmapping)

To transform flat data structures into nested object graphs matching JSON
schema definitions, this package defines a mapping language. It defines how
the columns of a source data set (e.g. a CSV file, database table) are to be
converted to the fields of a JSON schema.

The format allows mapping nested structures, including arrays. It also supports
the application of very basic data transformation steps, such as generating a
URL slug or hashing a column value.

## Example mapping

The mapping format is independent of any particular JSON schema, such that
multiple mappings could be defined for any one particular schema.

```json
{
"schema": {"$ref": "http://www.popoloproject.com/schemas/person.json"},
"mapping": {
"id": {"column": "person_id"},
"name": {"column": "person_name"},
"memberships": [{
"mapping": {
"role": {"default": "Member of Organization"},
"organization": {
"mapping": {
"id": {
"columns": ["org_id"],
"constant": "default-org"
},
"name": {
"column": "org_name",
"constant": "Default Organization",
"transforms": ["strip"]
}
}
}
}
}]
}
}
```

This mapping would apply to a four-column CSV file and map it to a set of
nested JSON objects (a [Popolo](http://www.popoloproject.com/) person, with a
membership in an organization).

## Data Transforms

While ``jsonmapping`` is not a data cleaning tool, it supports some very basic
data transformation operations that can be applied on a particular column or
set of columns. These include:

* ``coalesce``: Select the first non-null value from the list of items.
* ``slugify``: Transform each string into a URL slug form.
* ``join``: Merge together the string values of all selected columns.
* ``upper``: Transform the text to upper case.
* ``lower``: Transform the text to lower case.
* ``strip``: Remove leading and trailing whitespace.
* ``hash``: Generate a SHA1 hash of the given value.

## Usage

``jsonmapping`` is available on the Python Package Index:

```bash
$ pip install jsonmapping
```

The library can then be used as follows:

```python
from jsonschema import RefResolver
from jsonmapping import Mapper

# ... load the mapping ...
mapping = load_mapping()
resolver = RefResolver.from_schema(mapping)

# ... grab some data ...
rows = read_csv()
objs = []

# This will transform flat data rows into nested JSON objects:
for obj, err in Mapper.apply_iter(rows, mapping, resolver):
if err is None:
objs.append(obj)
```

## Tests

The test suite will usually be executed in it's own ``virtualenv`` and perform a
coverage check as well as the tests. To execute on a system with ``virtualenv``
and ``make`` installed, type:

```bash
$ make test
```

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jsonmapping-0.6.0.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

jsonmapping-0.6.0-py2-none-any.whl (13.1 kB view details)

Uploaded Python 2

File details

Details for the file jsonmapping-0.6.0.tar.gz.

File metadata

File hashes

Hashes for jsonmapping-0.6.0.tar.gz
Algorithm Hash digest
SHA256 b4481965f3ab5046b1bd6ed05533ab8b3a5ff76da369db4f1f0c9156faf65bb6
MD5 fb4190e6ef9aa36b44f1d50e4c983635
BLAKE2b-256 3848415abe6aa49bd77a1bfb872898855552f50b6ec98c4b68176bd41ae5dcfa

See more details on using hashes here.

File details

Details for the file jsonmapping-0.6.0-py2-none-any.whl.

File metadata

File hashes

Hashes for jsonmapping-0.6.0-py2-none-any.whl
Algorithm Hash digest
SHA256 e3b124632c578e2e3ce9c47f0d5e1e7444005bc46c8ef36dfba926c7e66b95f9
MD5 152a89a88cca0f0d619150e1db0c1d1b
BLAKE2b-256 f9e8bf449abbbfb3c44beed9909f9478caacd232653a3a0bfa2bd4d57b9a7a67

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page