def from_json(cls, path, row_names=None, key=None, newline=False, column_types=None, **kwargs): """ Create a new table from a JSON file. Once the JSON has been deseralized, the resulting Python object is passed to :meth:`.Table.from_object`. If the file contains a top-level dictionary you may specify what property contains the row list using the :code:`key` parameter. :code:`kwargs` will be passed through to :meth:`json.load`. :param path: Filepath or file-like object from which to read JSON data. :param row_names: See the :meth:`.Table.__init__`. :param key: The key of the top-level dictionary that contains a list of row arrays. :param newline: If `True` then the file will be parsed as "newline-delimited JSON". :param column_types: See :meth:`.Table.__init__`. """ from agate.table import Table if key is not None and newline: raise ValueError('key and newline may not be specified together.') if newline: js = [] if hasattr(path, 'read'): for line in path: js.append(json.loads(line, object_pairs_hook=OrderedDict, parse_float=Decimal, **kwargs)) else: with open(path, 'r') as f: for line in f: js.append(json.loads(line, object_pairs_hook=OrderedDict, parse_float=Decimal, **kwargs)) else: if hasattr(path, 'read'): js = json.load(path, object_pairs_hook=OrderedDict, parse_float=Decimal, **kwargs) else: with open(path, 'r') as f: js = json.load(f, object_pairs_hook=OrderedDict, parse_float=Decimal, **kwargs) if isinstance(js, dict): if not key: raise TypeError('When converting a JSON document with a top-level dictionary element, a key must be specified.') js = js[key] return Table.from_object(js, row_names=row_names, column_types=column_types)
def from_json(cls, path, column_names=None, column_types=None, keys=None, **kwargs): """ Create a new :class:`TableSet` from a directory of JSON files or a single JSON object with key value (Table key and list of row objects) pairs for each :class:`Table`. See :meth:`.Table.from_json` for additional details. :param path: Path to a directory containing JSON files or filepath/file-like object of nested JSON file. :param keys: A list of keys of the top-level dictionaries for each file. If specified, length must be equal to number of JSON files in path. :param column_types: See :meth:`Table.__init__`. """ from agate.tableset import TableSet if isinstance(path, six.string_types) and not os.path.isdir(path) and not os.path.isfile(path): raise IOError('Specified path doesn\'t exist.') tables = OrderedDict() if isinstance(path, six.string_types) and os.path.isdir(path): filepaths = glob(os.path.join(path, '*.json')) if keys is not None and len(keys) != len(filepaths): raise ValueError('If specified, keys must have length equal to number of JSON files') for i, filepath in enumerate(filepaths): name = os.path.split(filepath)[1].strip('.json') if keys is not None: tables[name] = Table.from_json(filepath, keys[i], column_types=column_types, **kwargs) else: tables[name] = Table.from_json(filepath, column_types=column_types, **kwargs) else: if hasattr(path, 'read'): js = json.load(path, object_pairs_hook=OrderedDict, parse_float=Decimal, **kwargs) else: with open(path, 'r') as f: js = json.load(f, object_pairs_hook=OrderedDict, parse_float=Decimal, **kwargs) for key, value in js.items(): tables[key] = Table.from_object(value, column_types=column_types, **kwargs) return TableSet(tables.values(), tables.keys())
def from_json(cls, path, row_names=None, key=None, newline=False, column_types=None, encoding='utf-8', **kwargs): """ Create a new table from a JSON file. Once the JSON has been deseralized, the resulting Python object is passed to :meth:`.Table.from_object`. If the file contains a top-level dictionary you may specify what property contains the row list using the :code:`key` parameter. :code:`kwargs` will be passed through to :meth:`json.load`. :param path: Filepath or file-like object from which to read JSON data. :param row_names: See the :meth:`.Table.__init__`. :param key: The key of the top-level dictionary that contains a list of row arrays. :param newline: If `True` then the file will be parsed as "newline-delimited JSON". :param column_types: See :meth:`.Table.__init__`. :param encoding: According to RFC4627, JSON text shall be encoded in Unicode; the default encoding is UTF-8. You can override this by using any encoding supported by your Python's open() function if :code:`path` is a filepath. If passing in a file handle, it is assumed you have already opened it with the correct encoding specified. """ from agate.table import Table if key is not None and newline: raise ValueError('key and newline may not be specified together.') close = False try: if newline: js = [] if hasattr(path, 'read'): for line in path: js.append( json.loads(line, object_pairs_hook=OrderedDict, parse_float=Decimal, **kwargs)) else: f = io.open(path, encoding=encoding) close = True for line in f: js.append( json.loads(line, object_pairs_hook=OrderedDict, parse_float=Decimal, **kwargs)) else: if hasattr(path, 'read'): js = json.load(path, object_pairs_hook=OrderedDict, parse_float=Decimal, **kwargs) else: f = io.open(path, encoding=encoding) close = True js = json.load(f, object_pairs_hook=OrderedDict, parse_float=Decimal, **kwargs) if isinstance(js, dict): if not key: raise TypeError( 'When converting a JSON document with a top-level dictionary element, a key must be specified.' ) js = js[key] finally: if close: f.close() return Table.from_object(js, row_names=row_names, column_types=column_types)