Class: QgsJsonUtils

class qgis.core.QgsJsonUtils

Bases: sip.wrapper

Helper utilities for working with JSON and GeoJSON conversions.

New in version 2.16: 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.
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.

Signals

Attributes

encodeValue(value: Any) → str

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

Parameters:value – value to encode
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 – feature to export
  • layer – optional associated vector layer. If specified, this allows richer export utilising settings like the layer’s fields widget configuration.
  • attributeWidgetCaches – optional widget configuration cache. Can be used to speed up exporting the attributes for multiple features from the same layer.
parseArray(json: str, type: QVariant.Type) → List[Any]

Parse a simple array (depth=1).

Parameters:
  • json – the JSON to parse
  • type – the type of the elements

New in version 3.0.

stringToFeatureList(string: str, fields: QgsFields, encoding: QTextCodec) → object

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

Parameters:
  • string – GeoJSON string to parse
  • fields – fields collection to use for parsed features
  • encoding – text encoding
Returns:

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()

stringToFields(string: str, encoding: QTextCodec) → QgsFields

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

Parameters:
  • string – GeoJSON string to parse
  • encoding – text encoding
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()