Class: QgsJsonUtils¶
- class qgis.core.QgsJsonUtils¶
Bases:
sip.wrapper
Helper utilities for working with JSON and GeoJSON conversions.
New in version 2.16:
Methods
Encodes a value to a JSON string representation, adding appropriate quotations and escaping where required.
Exports all attributes from a
QgsFeature
as a JSON map type.Parse a simple array (depth=1)
Attempts to parse a GeoJSON
string
to a collection of features.Attempts to retrieve the fields from a GeoJSON
string
representing a collection of features.Attributes
- encodeValue(value: Any) str ¶
Encodes a value to a JSON string representation, adding appropriate quotations and escaping where required.
- Parameters:
value (Any) – value to encode
- Return type:
str
- Returns:
encoded value
- exportAttributes(feature: QgsFeature, layer: QgsVectorLayer = None, attributeWidgetCaches: Iterable[Any] = []) str ¶
Exports all attributes from a
QgsFeature
as a JSON map type.- Parameters:
feature (QgsFeature) – feature to export
layer (
QgsVectorLayer
= None) – optional associated vector layer. If specified, this allows richer export utilising settings like the layer’s fields widget configuration.attributeWidgetCaches (Iterable[Any] = []) – optional widget configuration cache. Can be used to speed up exporting the attributes for multiple features from the same layer.
- Return type:
str
- parseArray(json: str, type: QVariant.Type = QVariant.Invalid) List[Any] ¶
Parse a simple array (depth=1)
- Parameters:
json (str) – the JSON to parse
type (QVariant.Type = QVariant.Invalid) – optional variant type of the elements, if specified (and not Invalid), the array items will be converted to the type, and discarded if the conversion is not possible.
New in version 3.0.
- Return type:
List[Any]
- staticMetaObject = <PyQt5.QtCore.QMetaObject object>¶
- stringToFeatureList(string: str, fields: QgsFields = QgsFields(), encoding: QTextCodec = None) List[QgsFeature] ¶
Attempts to parse a GeoJSON
string
to a collection of features. It is possible to specifyfields
to parse specific fields, if not provided, no fields will be included. Anencoding
can be specified which defaults to UTF-8 if it is None.- Return type:
List[QgsFeature]
- Returns:
a list of parsed features, or an empty list if no features could be parsed
See also
Note
this function is a wrapper around
QgsOgrUtils.stringToFeatureList()
- Parameters:
string (str) –
fields (
QgsFields
= QgsFields()) –encoding (QTextCodec = None) –
- stringToFields(string: str, encoding: QTextCodec = None) QgsFields ¶
Attempts to retrieve the fields from a GeoJSON
string
representing a collection of features. Anencoding
can be specified which defaults to UTF-8 if it is None.- Return type:
- Returns:
retrieved fields collection, or an empty list if no fields could be determined from the string
See also
Note
this function is a wrapper around
QgsOgrUtils.stringToFields()
- Parameters:
string (str) –
encoding (QTextCodec = None) –