Skip to content

incendium.dataset.to_jsonobject

César Román edited this page Sep 3, 2022 · 10 revisions

Description

Convert a Dataset into a Python list of dictionaries.

Syntax

incendium.dataset.to_json(dataset)

Args:

  • dataset (Dataset): The input dataset.

Returns:

  • list[dict]: The Dataset as a Python object.

Note

This is particular useful in Perspective when trying to transform a Dataset.

Recommendations

In order to properly output non-ASCII characters, prepend your strings with a u, i.e. u"Černá Hora", or import unicode_literals from __future__.

Code Examples

from __future__ import print_function, unicode_literals

import incendium.dataset
import system.dataset

# First create a list that contains the headers, in this case there are
# five headers.
headers = ["City", "Population", "Timezone", "GMTOffset", "Dataset"]

# Then create an empty list, this will house our data.
data = [
    [
        "北京市",
        21542000,
        "CST",
        +8,
        system.dataset.toDataSet(["id_", "value"], [[1, "one"], [2, "two"]]),
    ],
    ["Černá Hora", 2100, "CET", +1, None],
    ["Jundiaí", 423006, "BRT", -3, None],
    ["New York", 8336817, "EST", -5, None],
    ["Mazatlán", 502547, "MST", -7, None],
]

# Finally, both the headers and data lists are used in the function to
# create a Dataset object.
cities = system.dataset.toDataSet(headers, data)

# Print result
print(incendium.dataset.to_jsonobject(cities))

Output:

[{u'Timezone': u'CST', u'Dataset': [{u'id': 1, u'value': u'one'}, {u'id': 2, u'value': u'two'}], u'City': u'\u5317\u4eac\u5e02', u'Population': 21542000, u'GMTOffset': 8}]
Clone this wiki locally