Skip to main content
2 of 5
added 12 characters in body
Kadir Şahbaz
  • 78k
  • 57
  • 257
  • 404

QgsJsonUtils class has exportAttributes method for getting attributes as dict.

Solution 1:

Making a dictionary including feature id as key, attributes map as value like:

{feature1.id: {attr1: value, attr2: value, ...},
 feature2.id: {attr1: value, attr2: value, ...},
 ...}
layer = QgsProject.instance().mapLayersByName('LAYER_NAME')[0]
features_dict = {f.id(): QgsJsonUtils.exportAttributes(f) for f in layer.getFeatures()}

Solution 2:

Making a list including attributes map (dict) of features without ids like:

[{attr1: value, attr2: value, ...}, # feature 1
 {attr1: value, attr2: value, ...}, # feature 2
 ...]
layer = QgsProject.instance().mapLayersByName('LAYER_NAME')[0]
features_list = [QgsJsonUtils.exportAttributes(f) for f in layer.getFeatures()]
Kadir Şahbaz
  • 78k
  • 57
  • 257
  • 404