How do I modify and combine JSON objects in EK9?

← JSON and Data Processing · Ref: Q192

EK9 provides operators for building and combining JSON objects. Use constructors to create objects and operators to merge, replace, or add properties.

BUILDING JSON OBJECTS

Create an empty JSON object:

  obj <- JSON().object()

Create with key-value pairs:

  person <- JSON("name", JSON("Alice"))

ADDING PROPERTIES (+)

Add a key-value pair to a JSON object:

  person + JSON("age", JSON(30))

MERGE OPERATOR (:~:)

Merge properties from one JSON object into another:

  base :~: extras

Existing properties in base are kept, new properties from extras are added.

REPLACE OPERATOR (:^:)

Replace matching properties:

  target :^: replacement

Properties in target that also exist in replacement are overwritten.

See Q188 for JSON basics. See Q96 for operator semantics. See Q189 for converting to JSON.

Example

defines module qa.jsondata.manipulate

  defines program

    JsonManipulateDemo()
      stdout <- Stdout()

      // === BUILDING JSON OBJECTS ===

      emptyObj <- JSON().object()
      stdout.println(`Empty object: ${emptyObj}`)

      person <- JSON("name", JSON("Alice"))
      stdout.println(`Person: ${person}`)

      // === ADDING PROPERTIES WITH + ===

      updated <- person + JSON("age", JSON(30))
      stdout.println(`With age: ${updated}`)

      // === MERGE OPERATOR :~: ===

      base <- JSON(`{"name": "Alice", "role": "dev"}`)
      extras <- JSON(`{"team": "backend", "level": 5}`)
      base :~: extras
      stdout.println(`After merge: ${base}`)

      // === REPLACE OPERATOR :^: ===

      target <- JSON(`{"name": "Alice", "age": 30}`)
      replacement <- JSON(`{"age": 31, "city": "London"}`)
      target :^: replacement
      stdout.println(`After replace: ${target}`)

Common mistakes

E50001 — The JSON type does not have a static 'createObject()' method. Create an empty JSON object by calling 'JSON().object()' which creates an unset JSON and then converts it to an empty object. See ek9 -h E50001 for details.

Incorrect:

emptyObj <- JSON.createObject()

Correct:

emptyObj <- JSON().object()
Other ways to ask this
  • How do I merge two JSON objects in EK9?
  • How do I build a JSON object from parts in EK9?
  • How do I add properties to JSON in EK9?

Coming from another language?

Java: Jackson ObjectNode.put()/set() for building, JsonNode merge via custom code. Python: dict.update() or {**a, **b} merge. Rust: serde_json::Map insert/extend. Go: manual map merging. Kotlin: mutableMapOf + putAll. EK9: :~: merge and :^: replace operators on JSON, + for adding properties.

Keywords: manipulate, json, replace, object, combine, property, merge, data, serialize, add, build