Skip to content

incendium.dataset.to_jsonobject

César Román edited this page Apr 30, 2024 · 9 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}, {u'Timezone': u'CET', u'Dataset': None, u'City': u'\u010cern\xe1 Hora', u'Population': 2100, u'GMTOffset': 1}, {u'Timezone': u'BRT', u'Dataset': None, u'City': u'Jundia\xed', u'Population': 423006, u'GMTOffset': -3}, {u'Timezone': u'EST', u'Dataset': None, u'City': u'New York', u'Population': 8336817, u'GMTOffset': -5}, {u'Timezone': u'MST', u'Dataset': None, u'City': u'Mazatl\xe1n', u'Population': 502547, u'GMTOffset': -7}]
Clone this wiki locally