> ## Documentation Index
> Fetch the complete documentation index at: https://docs.superoffice.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Struct

> A struct defines the kinds of data and the functionality their objects will have. This data structure enables you to create your own custom types by grouping together variables of other types and methods.

A struct defines the kinds of data and the functionality their objects will have. This data structure enables you to create your own custom types by grouping together variables of other types and methods.

## Methods

## compare

If a struct has a function with this exact signature, then this method will be used to compare two instances of the struct when sorting an array of struct instances.

```crmscript theme={null}
Bool {type}.compare({type} s)
```

**Example:**

```crmscript theme={null}
struct Person {
  String firstname;
  String lastname;
  Bool compare(Person p) {
    return this.toString() > p.toString();
  }
};
Person Person(String firstname, String lastname) {
  Person p;
  p.firstname = firstname;
  p.lastname = lastname;
  return p;
}
Person[] persons;
persons.pushBack(Person(\"Jack\", \"Black\"));
persons.pushBack(Person(\"Mark\", \"Wahlberg\"));
persons.pushBack(Person(\"Anthony\", \"Hopkins\"));
printLine(\"Before sort: \" + persons.buildString(\"|\"));
persons.sort();
printLine(\"After sort: \" + persons.buildString(\"|\"));
```

## fromJsonString

Pass a boolean value to copy into a new object.

```crmscript theme={null}
Void {type}.fromJsonString(String json)
```

**Example:**

```crmscript theme={null}
struct Person {
  String firstname;
  String lastname;
  Date dob;
};
Person p;
p.fromJsonString('{\"firstname\": \"Jon\", \"lastname\": \"Doe\", \"dob\": \"1984-10-26\"}');
printLine(p.toJsonString());
```

## fromXMLNode

Populates a struct from an XMLNode.

```crmscript theme={null}
Void {type}.fromXMLNode(XMLNode node)
```

**Example:**

```crmscript theme={null}
struct Hello {
  String who;
  Void print() {
    printLine(\"Hello \" + this.who);
  }
};
XMLNode xml = parseXML(\"<root><who>World</who></root>\");
Hello h;
h.fromXMLNode(xml);
h.print();
//prints Hello World
```

## toJson

Populates a JSONBuilder from a struct.

```crmscript theme={null}
Void {type}.toJson(JSONBuilder builder)
```

**Example:**

```crmscript theme={null}
struct Hello {
  String who;
  Void setWho(String who) {
    this.who = who;
  }
  Void print() {
    printLine(\"Hello \" + this.who);
  }
};
JSONBuilder jsBuilder;
Hello h;
h.setWho(\"World\");
h.toJson(jsBuilder);
print(jsBuilder.getString());
//prints {\"who\": \"World\"}
```

## toJsonString

This function will return a JSON formatted string of the struct's contents. Same functionality as toJson(JSONBuilder), but it will return a string directly without having to instantiate a JSONbuilder.

```crmscript theme={null}
String {type}.toJsonString()
```

**Example:**

```crmscript theme={null}
struct Person {
  String firstname;
  String lastname;
    Date dob;
};
Person p;
p.firstname = \"Jack\";
p.lastname = \"Black\";
p.dob = Date(\"1975-11-28\");
printLine(p.toJsonString());
//prints {\"firstname\": \"Jack\",\"lastname\": \"Black\",\"dob\": \"1975-11-28\"}
```

## toString

This function will return a JSON formatted string of the struct's contents. Same functionality as toJson(JSONBuilder), but it will return a string directly without having to instantiate a JSONbuilder.

```crmscript theme={null}
String {type}.toString()
```

**Example:**

```crmscript theme={null}
struct Person {
  String firstname;
  String lastname;
  String toString() {
    return this.firstname + \" \" + this.lastname;
  }
};
Person p;
p.firstname = \"Jack\";
p.lastname = \"Black\";
printLine(p.toString());
//prints Jack Black
```


## Related topics

- [Structs](/en/automation/crmscript/fundamentals/structs.md)
- [SuperOffice.WebApi.Data.StructuredAddress](/en/api/reference/webapi/SuperOffice.WebApi.Data.StructuredAddress.md)
- [Generic data type](/en/automation/crmscript/datatypes/generic-type.md)
- [Code structure (syntax)](/en/automation/crmscript/fundamentals/syntax.md)
- [CRMScript coding conventions](/en/automation/crmscript/code-quality/coding-conventions.md)
- [Void](/en/automation/crmscript/reference/CRMScript.Global.Void.md)
