Object.toString()

Convert an Object to a String.

Signature

Object.toString(object: Any) => String

Description

Object.toString() creates a String representation of a provided Object. This method is useful for debugging, logging, or when you need a String representation of complex Objects.

Parameters

Parameter Type Required Description

object

Any

Yes

Object to convert to a String.

Return value

Type Description

String

String representation of the Object. Special characters are properly escaped.

Examples

Object.toString({a: "test"})
"{ a: \"test\" }"

Nested Object

// Defines an Object with customer data.
let customer =  {
  "name": "Ruby Von Rails",
  "email": "ruby@example.com",
  "address": {
    "city": "Washington",
    "state": "DC"
  }
}

Object.toString(customer)
"{ name: \"Ruby Von Rails\", email: \"ruby@example.com\", address: { city: \"Washington\", state: \"DC\" } }"

Object with Multiple Data Types

// Defines an Object.
let data = {
  string: "Hello",
  number: 42,
  boolean: true,
  null: null,
  array: [1, 2, 3]
}

Object.toString(data)
"{ string: \"Hello\", number: 42, boolean: true, null: null, array: [1, 2, 3] }"

Objects containing document references

// Defines an Object.
let data = {
  // Contains a reference to a Category
  // collection document.
  category: Category.byName("produce").first(),
}

Object.toString(data)
"{ category: { id: ID(\"410674590631592013\"), coll: Category, ts: Time(\"2099-10-02T22:37:39.583Z\"), products: [set], name: \"produce\", description: \"Fresh Produce\" } }"

Objects containing Set references

// Defines an Object.
let data = {
  // Contains a reference to a Set of all
  // Category collection documents.
  category: Category.all(),
}

Object.toString(data)
"{ category: [set] }"

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!