Populating and Extracting Data

This guide shows you how to populate form fields and extract data from completed forms.

Accessing Fields

Use the DocumentEditor.fields property to access form fields.

Finding Fields

val editor = rememberDocumentEditor(json)

// Find by ID, identifier, or title
val nameField = editor.fields.find("field_id")
val emailField = editor.fields.find("Email Address") // by title

// Find specific field types
val textField = editor.fields.text("Full Name")
val numberField = editor.fields.number("Age")
val imageField = editor.fields.image("Profile Picture")
val tableField = editor.fields.table("Employee Data")

Extracting Data

Document-Level Extraction

// Extract as Document object
val document: Document = editor.toDocument()

// Extract as JSON string
val jsonString: String = editor.toJsonString()

// Extract as Map
val dataMap: Map<String, Any?> = editor.toMap()

Field-Level Extraction

// Get individual field values
val name = editor.fields.text("Full Name")?.state?.value?.data
val age = editor.fields.number("Age")?.state?.value?.data
val email = editor.fields.text("Email")?.state?.value?.data
val profileImages = editor.fields.image("Profile Picture")?.state?.value?.data

Note: To get a reactive state, use state.watchAsLive().

Working with Tables

Populating Table Data

// Get table editor
val employeeTable = editor.fields.table("Employees")

// Add new rows and populate data
employeeTable?.let { table ->
    // Add a new row
    val newRow = table.rows.append()
    
    // Populate row cells
    newRow.text("First Name")?.value("John")
    newRow.text("Last Name")?.value("Doe")
    newRow.number("Salary")?.value(75000.0)
    newRow.dropdown("Department")?.value("Engineering")
}

Extracting Table Data

fun extractTableData(editor: DocumentEditor): List<Map<String, Any?>> {
    val table = editor.fields.table("Employees") ?: return emptyList()
    
    return table.rows.all().map { row ->
        mapOf(
            "firstName" to row.text("First Name")?.state?.value?.data,
            "lastName" to row.text("Last Name")?.state?.value?.data,
            "salary" to row.number("Salary")?.state?.value?.data,
            "department" to row.dropdown("Department")?.state?.value?.data?.value
        )
    }
}

Document Properties

Setting Document Properties

// Set document-level properties
editor.name = "User Registration Form"
editor.identifier = "user_reg_2024"
editor.stage = Stage.published

// Set custom properties
editor.set("customField", "customValue")
editor.set("metadata", mapOf("version" to "1.0"))

Getting Document Properties

// Get document properties
val documentName = editor.name
val documentId = editor.id
val documentStage = editor.stage

// Get custom properties
val customValue: String = editor.get("customField")
val metadata: Map<String, Any> = editor.get("metadata")

Things to Know

Use editor.fields.find() or type-specific methods like text() and number() to access fields. Field changes automatically update the underlying document state. To extract complete form data, use toDocument()toJsonString(), or toMap(). Cast field values to appropriate types when extracting data. Tables require row-level access for both population and extraction.


What’s Next