Json¶
-
supervisely_lib.io.json.
load_json_file
(filename: str) → dict[source]¶ Decoding data from json file with given filename.
- Parameters
filename (str) – Target file path.
- Returns
Json format as a dict
- Return type
dict
- Usage example
from supervisely_lib.io.json import load_json_file json_example = load_json_file('/home/admin/work/projects/examples/ann.json') print(json_example) # Output: { # "description": "", # "tags": [], # "size": { # "height": 800, # "width": 1067 # }, # "objects": [ # { # "id": 619053179, # "classId": 2791451, # "description": "", # "geometryType": "bitmap", # "labelerLogin": "alexxx", # "createdAt": "2021-02-10T08:36:33.898Z", # "updatedAt": "2021-02-10T08:39:24.828Z", # "tags": [], # "classTitle": "lemon", # "bitmap": { # "data": "eJwBZgOZ/IlQTkcNChoKAAAADUlIR...AiRp9+EwAAAABJRU5ErkJgggn2cM0=", # "origin": [ # 531, # 120 # ] # } # }, # { # "id": 619053347, # "classId": 2791453, # "description": "", # "geometryType": "rectangle", # "labelerLogin": "alexxx", # "createdAt": "2021-02-10T08:39:08.369Z", # "updatedAt": "2021-02-10T08:39:24.828Z", # "tags": [], # "classTitle": "kiwi", # "points": { # "exterior": [ # [ # 764, # 387 # ], # [ # 967, # 608 # ] # ], # "interior": [] # } # }, # { # "id": 619053355, # "classId": 2791453, # "description": "", # "geometryType": "rectangle", # "labelerLogin": "alexxx", # "createdAt": "2021-02-10T08:39:16.938Z", # "updatedAt": "2021-02-10T08:39:24.828Z", # "tags": [], # "classTitle": "kiwi", # "points": { # "exterior": [ # [ # 477, # 543 # ], # [ # 647, # 713 # ] # ], # "interior": [] # } # } # ] # }
-
supervisely_lib.io.json.
dump_json_file
(data: dict, filename: str, indent: int = 4) → None[source]¶ Write given data in json format in file with given name.
- Parameters
data (dict) – Data in json format as a dict.
filename (str) – Target file path to write data.
indent (int, optional) – Json array elements and object members will be pretty-printed with that indent level.
- Returns
None
- Return type
NoneType
- Usage example
from supervisely_lib.io.json import dump_json_file data = {1: 'example'} dump_json_file(data, '/home/admin/work/projects/examples/1.json')
-
supervisely_lib.io.json.
flatten_json
(data: dict, sep: str = '.') → dict[source]¶ Normalize semi-structured JSON data into a flat table.
- Parameters
data (dict) – Data in json format as a dict.
sep (str, optional) – Nested records will generate names separated by sep.
- Returns
None
- Return type
NoneType
-
supervisely_lib.io.json.
modify_keys
(data: dict, prefix: Optional[str] = None, suffix: Optional[str] = None) → dict[source]¶ Add prefix and suffix to keys of given dict.
- Parameters
data (dict) – Data in json format as a dict.
prefix (str, optional) – Prefix which will be added to dict.
suffix (str, optional) – Suffix which will be added to dict.
- Returns
New dict with prefix and suffix in keys
- Return type
dict
- Usage example
from supervisely_lib.io.json import modify_keys data = {'1': 'example', '3': 4} new_data = modify_keys(data, prefix='pr_', suffix='_su') print(new_data) # Output: {'pr_1_su': 'example', 'pr_3_su': 4}