Example #1
0
def from_json(json_data):
    """transform a json structure into jsonstat objects hierarchy

    :param json_data: data structure (dictionary) representing a json
    :returns: a JsonStatCollection, JsonStatDataset or JsonStatDimension object

    >>> import json, jsonstat
    >>> from collections import OrderedDict
    >>> json_string_v1 = '''{
    ...                       "oecd" : {
    ...                         "value": [1],
    ...                         "dimension" : {
    ...                           "id": ["one"],
    ...                           "size": [1],
    ...                           "one": { "category": { "index":{"2010":0 } } }
    ...                         }
    ...                       }
    ...                     }'''
    >>> json_data = json.loads(json_string_v1, object_pairs_hook=OrderedDict)
    >>> jsonstat.from_json(json_data)
    JsonstatCollection contains the following JsonStatDataSet:
    +-----+---------+
    | pos | dataset |
    +-----+---------+
    | 0   | 'oecd'  |
    +-----+---------+

    """
    o = None
    if "version" in json_data:
        # if version present assuming version 2 of jsonstat format
        if "class" in json_data:
            if json_data["class"] == "collection":
                o = JsonStatCollection()
                o._from_json_v2(json_data)
            elif json_data["class"] == "dataset":
                o = JsonStatDataSet()
                o._from_json_v2(json_data)
            elif json_data["class"] == "dimension":
                o = JsonStatDimension()
                o.from_json(json_data)
            else:
                msg = "unknown class {}".format(json_data["class"])
                raise JsonStatMalformedJson(msg)

    else:
        # if version is not present assuming version 1.0 of jsonstat format
        o = JsonStatCollection()
        o._from_json_v1(json_data)
    return o
Example #2
0
    def _from_json_v2(self, json_data):
        """parse a jsonstat version 2

        .. warning::

            this is an internal library function (it is not public api)

        :param json_data: json structure


        json_data example::

            "version" : "2.0",
            "class" : "collection",
            "href" : "http://json-stat.org/samples/collection.json",
            "label" : "JSON-stat Dataset Sample Collection",
            "updated" : "2015-07-02",
            "link" : {
               "item" : [
                    {},
                    {},
                ]
        }

        """

        if "href" in json_data:
            self.__href = json_data["href"]

        if "label" in json_data:
            self.__label = json_data["label"]

        if "updated" in json_data:
            self.__updated = dateutil.parser.parse(json_data["updated"])

        json_data_ds = json_data["link"]["item"]
        self.__pos2dataset = len(json_data_ds) * [None]
        for pos, ds in enumerate(json_data_ds):
            dataset = JsonStatDataSet()
            dataset._from_json_v2(ds)
            self.__pos2dataset[pos] = dataset
Example #3
0
    def _from_json_v1(self, json_data):
        """parse a jsonstat version 1

        .. warning::

            this is an internal library function (it is not public api)

        :param json_data: json structure

        json_data example::

            {
                "dataset1" : {...}
                "dataset2" : {...}
            }

        """
        for dataset_name, dataset_json in json_data.items():
            dataset = JsonStatDataSet(dataset_name)
            dataset.from_json(dataset_json)
            self.__name2dataset[dataset_name] = dataset
            self.__pos2dataset.append(dataset)