Skip to content

incendium.dataset.to_json

César Román edited this page Apr 30, 2024 · 16 revisions

Description

Return a string JSON representation of the Dataset.

Syntax

incendium.dataset.to_json(dataset, [root])

Args:

  • dataset (Dataset): The input dataset.
  • root (str): The value of the root. Optional.

Returns:

  • str: The string JSON representation of the 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_json(cities, "cities"))

Output:

{"cities":[{"City":"北京市","Population":21542000,"Timezone":"CST","GMTOffset":8,"Dataset":[{"id":1,"value":"one"},{"id":2,"value":"two"}]},{"City":"Černá Hora","Population":2100,"Timezone":"CET","GMTOffset":1,"Dataset":null},{"City":"Jundiaí","Population":423006,"Timezone":"BRT","GMTOffset":-3,"Dataset":null},{"City":"New York","Population":8336817,"Timezone":"EST","GMTOffset":-5,"Dataset":null},{"City":"Mazatlán","Population":502547,"Timezone":"MST","GMTOffset":-7,"Dataset":null}]}
Clone this wiki locally