Class: QgsJsonUtils

Helper utilities for working with JSON and GeoJSON conversions.

Static Methods

encodeValue

Encodes a value to a JSON string representation, adding appropriate quotations and escaping where required.

exportAttributes

Exports all attributes from a QgsFeature as a JSON map type.

geometryFromGeoJson

Parses a GeoJSON "geometry" value to a QgsGeometry object.

parseArray

Parse a simple array (depth=1)

stringToFeatureList

Attempts to parse a GeoJSON string to a collection of features.

stringToFields

Attempts to retrieve the fields from a GeoJSON string representing a collection of features.

class qgis.core.QgsJsonUtils[source]

Bases: object

static encodeValue(value: Any) str[source]

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

static exportAttributes(feature: QgsFeature, layer: QgsVectorLayer | None = None, attributeWidgetCaches: Iterable[Any] = []) str[source]

Exports all attributes from a QgsFeature as a JSON map type.

Parameters:
  • feature (QgsFeature) – feature to export

  • layer (Optional[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

static geometryFromGeoJson(geometry: str | None) QgsGeometry[source]

Parses a GeoJSON “geometry” value to a QgsGeometry object.

Returns a null geometry if the geometry could not be parsed.

Added in version 3.36.

Parameters:

geometry (Optional[str])

Return type:

QgsGeometry

static parseArray(json: str | None, type: QMetaType.Type = QMetaType.Type.UnknownType) List[Any][source]

Parse a simple array (depth=1)

Parameters:
  • json (Optional[str]) – the JSON to parse

  • type (QMetaType.Type = QMetaType.Type.UnknownType) – 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.

Return type:

List[Any]

static parseArray(json: str | None, type: QVariant.Type) List[Any][source]

Parse a simple array (depth=1)

Parameters:
  • json (Optional[str]) – the JSON to parse

  • type (QVariant.Type) – 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.

Deprecated since version 3.38: Use the method with a QMetaType.Type argument instead.

Return type:

List[Any]

static stringToFeatureList(string: str | None, fields: QgsFields = QgsFields(), encoding: QTextCodec | None = None) List[QgsFeature][source]

Attempts to parse a GeoJSON string to a collection of features. It is possible to specify fields to parse specific fields, if not provided, no fields will be included. An encoding 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

stringToFields()

Note

this function is a wrapper around QgsOgrUtils.stringToFeatureList()

Parameters:
  • string (Optional[str])

  • fields (QgsFields = QgsFields())

  • encoding (Optional[QTextCodec] = None)

static stringToFields(string: str | None, encoding: QTextCodec | None = None) QgsFields[source]

Attempts to retrieve the fields from a GeoJSON string representing a collection of features. An encoding can be specified which defaults to UTF-8 if it is None.

Return type:

QgsFields

Returns:

retrieved fields collection, or an empty list if no fields could be determined from the string

Note

this function is a wrapper around QgsOgrUtils.stringToFields()

Parameters:
  • string (Optional[str])

  • encoding (Optional[QTextCodec] = None)