json

Classes

JsonSerializable

Functions

dump_json_file(data, filename[, indent])

Write given data in json format in file with given name.

flatten_json(data[, sep])

Normalize semi-structured JSON data into a flat table.

load_json_file(filename)

Decoding data from json file with given filename.

modify_keys(data[, prefix, suffix])

Add prefix and suffix to keys of given dict.

Description

dump_json_file(data, filename, indent=4)[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.io.json import dump_json_file
data = {1: 'example'}
dump_json_file(data, '/home/admin/work/projects/examples/1.json')
flatten_json(data, sep='.')[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

Dict

Return type

dict

load_json_file(filename)[source]

Decoding data from json file with given filename.

Parameters
filename : str

Target file path.

Raises
Returns

Json format as a dict

Return type

dict

Usage example
from supervisely.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": []
#             }
#         }
#     ]
# }
modify_keys(data, prefix=None, suffix=None)[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.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}