The JsonDataModel class
JsonDataModel
Bases: DataModelOperators, DataModelTransforms
A backend-independent dynamic data model.
This structure is the one flowing in the pipelines as the backend data models are only used for the variable/data model declaration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
dict
|
The JSON object's schema. If not provided, uses the data model to infer it. |
None
|
json
|
dict
|
The JSON object's json. If not provided, uses the data model to infer it. |
None
|
data_model
|
DataModel | JsonDataModel
|
The data model to use to infer the schema and json. |
None
|
name
|
str
|
Optional. The name of the data model, automatically inferred if not provided. |
None
|
Examples:
Creating a JsonDataModel with a DataModel's schema and json:
class Query(synalinks.DataModel):
query: str = synalinks.Field(
description="The user query",
)
json = {"query": "What is the capital of France?"}
data_model = JsonDataModel(
schema=Query.get_schema(),
json=json,
)
Creating a JsonDataModel with a data_model:
class Query(synalinks.DataModel):
query: str = synalinks.Field(
description="The user query",
)
query_instance = Query(
query="What is the capital of France?"
)
data_model = JsonDataModel(
data_model=query_instance,
)
Creating a JsonDataModel with to_json_data_model():
class Query(synalinks.DataModel):
query: str = synalinks.Field(
description="The user query",
)
data_model = Query(
query="What is the capital of France?",
).to_json_data_model()
Source code in synalinks/src/backend/common/json_data_model.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | |
__getitem__(key)
Get item wrapper to make it easier to access JSON fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to access. |
required |
clone(name=None)
Clone a data model and give it a different name.
get(key, default=None)
Get wrapper to make it easier to access JSON fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to access. |
required |
default
|
any
|
The default value if key not found. |
None
|
Source code in synalinks/src/backend/common/json_data_model.py
get_json()
Gets the current json of the JSON object.
Returns:
| Type | Description |
|---|---|
dict
|
The current json of the JSON object. |
get_nested_entity(key)
Retrieve a nested Entity and convert it to a JsonDataModel.
The entity's label field is used as the discriminator to look up
its schema in the parent's $defs. When the label value isn't a
$defs key (the free-form case, where label is open data rather
than a Literal matching the class name), the field's declared
schema (its $ref) is used as a fallback. Returns None when the
value at key has no label or when no schema resolves.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The field name holding the nested entity. |
required |
Returns:
| Type | Description |
|---|---|
JsonDataModel | None
|
A typed JsonDataModel wrapping the
nested entity, or |
Source code in synalinks/src/backend/common/json_data_model.py
get_nested_entity_list(key)
Retrieve a nested Entity list and convert it to typed JsonDataModels.
Each item is resolved against the parent's $defs using its
label field as discriminator. When the label value isn't a
$defs key (the free-form case, where label is open data rather
than a Literal matching the class name), the list's declared
item schema (items.$ref) is used as a fallback. Items without a
label field or with no resolvable schema are skipped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The field name holding the list of nested entities. |
required |
Returns:
| Type | Description |
|---|---|
list[JsonDataModel]
|
One JsonDataModel per resolved entity. |
Source code in synalinks/src/backend/common/json_data_model.py
get_schema()
items()
keys()
prettify_json()
Get a pretty version of the JSON object for display.
Returns:
| Type | Description |
|---|---|
str
|
The indented JSON object. |
Source code in synalinks/src/backend/common/json_data_model.py
prettify_schema()
Get a pretty version of the JSON schema for display.
Returns:
| Type | Description |
|---|---|
dict
|
The indented JSON schema. |
Source code in synalinks/src/backend/common/json_data_model.py
to_symbolic_data_model()
Converts the JsonDataModel to a SymbolicDataModel.
Returns:
| Type | Description |
|---|---|
SymbolicDataModel
|
The symbolic data model. |
update(kv_dict)
Update wrapper to make it easier to modify JSON fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kv_dict
|
dict
|
The key/json dict to update. |
required |
is_json_data_model(x)
Returns whether x is a backend-independent data model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
any
|
The object to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if |