Skip to main content

What is dot notation?

Dot notation is a way to access specific properties of an object or array using a period (.) to separate the property name from the object or array.

Using dot notation with objects

Let’s say we have the following object:
{
    "name": "John",
    "age": 30,
    "created_at": 1736364473744
}
If we want to use an Update Variable function to update that created_at property to be human readable, we would use dot notation to target that property directly, like this:
dot-notation-20251013-093904
This will target created_at inside of the object and update it to Wed, 08 Jan 2025 19:27:53 +0000. You can also use dot notation to create new properties inside of an object. Our user object doesn’t have a location property yet, so we can add it like this:
dot-notation-20251013-094029
So, even though the email property doesn’t exist, using dot notation to target it will create it for us.

Using dot notation with arrays

Arrays are a little different than objects in that they are indexed, starting at 0. So, the first item in the array is at index 0, the second item is at index 1, and so on. The index just refers to the position of the item in the array. Let’s say we have the following array:
[
    "apple", // index 0
    "banana", // index 1
    "cherry" // index 2
]
If we want to use an Update Variable function to update the second item in the array to be “orange”, we would use dot notation to target that item directly, like this:
dot-notation-20251013-094238
This will target the second item in the array and update it to orange.

Using dot notation with complex nested data

You can use dot notation with any combination of nested arrays and objects. Let’s say we have the following array:
[
    {
        "name": "apple",
        "details": {
            "color": "red",
            "price": 1.00
        }
    },
    {
        "name": "banana",
        "details": {
            "color": "yellow",
            "price": 0.50
        }
    }
]
If we want to use an Update Variable function to update the price of the second item in the array to be $0.75, we would use dot notation to target that item directly, like this:
dot-notation-20251013-094542
This will target the second item in the array and update the price to $0.75.

Try it out

You can create a new API or custom function and paste the following XanoScript into the editor to see it in action. Feel free to experiment and modify the logic to get a feel for how it works.
Dot notation example
query dot_notation_example verb=GET {
  input {
  }

  stack {
    var $user {
      value = {}
        |set:"name":"John"
        |set:"age":30
        |set:"created_at":1736364473744
    }
  
    var.update $user.created_at {
      value = $user.created_at|format_timestamp:"r":"UTC"
    }
  
    var.update $user.email {
      value = "john@email.com"
    }
  
    var $fruits {
      value = []
        |push:"apple"
        |push:"banana"
        |push:"cherry"
    }
  
    var.update $fruits.1 {
      value = "orange"
    }
  
    var $fruits_complex {
      value = """
        [
          {
            "name": "apple",
            "details": {
              "color": "red",
              "price": 1
            }
          },
          {
            "name": "banana",
            "details": {
              "color": "yellow",
              "price": 0.5
            }
          }
        ]
        """|json_decode
    }
  
    var.update $fruits_complex.1.details.price {
      value = 0.75
    }
  }

  response {
    value = {
      user          : $user
      fruits        : $fruits
      fruits_complex: $fruits_complex
    }
  }

  history = {inherit: true}
}
I