-
-
Notifications
You must be signed in to change notification settings - Fork 2
incendium.dataset.to_json
César Román edited this page Feb 25, 2021
·
17 revisions
Returns a string JSON representation of the Dataset.
to_json(dataset[, root])
Args:
- dataset (Dataset): The input dataset.
- root (str): The value of the root. If not provided, it defaults to "json". Optional.
Returns:
- str: The string JSON representation of the dataset.
In order to properly output non-ASCII characters, prepend your strings with a u
. For example: u"Černá Hora"
.
# Import
import incendium.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 = []
# Then add each row to the list. Note that each row is also a list object.
data.append([u"北京市", 21542000, "CST", +8, system.dataset.toDataSet(["id", "value"], [[1, "one"], [2, "two"]])])
data.append([u"Černá Hora", 2100, "CET", +1, None])
data.append([u"Jundiaí", 423006, "BRT", -3, None])
data.append(["New York", 8336817, "EST", -5, None])
data.append([u"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}]}