ToObject

ToObject( array )
ToObject( array )
ToObject( array )
ToObject( array )
to_object( array )
ToObject( array )

Description

The ToObject function converts an Array of arrays, containing field names and values, into an Object, if possible.

If array cannot be converted into an object, an "invalid argument" error is returned.

Parameters

Parameter Type Definition and Requirements

array

An Array of Arrays containing field names and values.

Returns

An Object composed of the fields names and values from array.

Examples

The following query converts an array into an object:

try
{
    Value result = await client.Query(
        ToObject(
            Arr(
                Arr("name", "Dennis"),
                Arr("age", 37)
            )
        )
    );

    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
ObjectV(name: StringV(Dennis),age: LongV(37))
result, err := client.Query(
	f.ToObject(
		f.Arr{
			f.Arr{"name", "Dennis"},
			f.Arr{"age", 37},
		}))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
map[age:37 name:Dennis]
System.out.println(
    client.query(
        ToObject(
            Arr(
                Arr(Value("name"), Value("Dennis")),
                Arr(Value("age"), Value(37))
            )
        )
    ).get());
{name: "Dennis", age: 37}
client.query(
  q.ToObject([['name', 'Dennis'], ['age', 37]])
)
.then((ret) => console.log(ret))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
{ name: 'Dennis', age: 37 }
result = client.query(
  q.to_object([['name', 'Dennis'], ['age', 37]])
)
print(result)
{'name': 'Dennis', 'age': 37}
ToObject([['name', 'Dennis'], ['age', 37]])
{ name: 'Dennis', age: 37 }
Query metrics:
  •    bytesIn:  44

  •   bytesOut:  39

  • computeOps:   1

  •    readOps:   0

  •   writeOps:   0

  •  readBytes:   0

  • writeBytes:   0

  •  queryTime: 2ms

  •    retries:   0

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!