Advanced usage | schema-org-go | Spatie       

 SPATIE  

schema-org-go
=============

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Schema-org-go](https://spatie.be/docs/schema-org-go/v1)  Advanced usage

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/schema-org-go/v1) 

  Advanced usage    
- [ Introduction ](https://spatie.be/docs/schema-org-go/v1/introduction)
- [ Installation and setup ](https://spatie.be/docs/schema-org-go/v1/installation-and-setup)
- [ Usage ](https://spatie.be/docs/schema-org-go/v1/usage)
- [ Advanced usage ](https://spatie.be/docs/schema-org-go/v1/advanced-usage)
- [ Graph - multiple items ](https://spatie.be/docs/schema-org-go/v1/graphs)
- [ Multi typed entities ](https://spatie.be/docs/schema-org-go/v1/multi-typed-entities)
- [ Generating types ](https://spatie.be/docs/schema-org-go/v1/generating-types)
- [ Testing ](https://spatie.be/docs/schema-org-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/schema-org-go/v1/releasing)
- [ Changelog ](https://spatie.be/docs/schema-org-go/v1/changelog)
- [ Support us ](https://spatie.be/docs/schema-org-go/v1/support-us)

 Advanced usage
==============

If you need to set a custom property, use `SetProperty("foo", "bar")`. To retrieve a property, use `GetProperty("name")`. You can pass a fallback with `GetProperty("bar", "baz")`. An absent property without a fallback returns nil.

All explicitly assigned properties can be retrieved as a map with `GetProperties`. Multiple properties can be set at once with `AddProperties(map[string]any{"name": "value", "foo": "bar"})`. The returned map is a shallow copy, so changing its membership does not change the builder. Nested entities remain shared.

Context and type can be retrieved with `GetContext` and `GetType`. For custom types, use `schema.New("MyType")`. For custom serialization, embed `*schema.Entity` and implement `ToArray`.

```
package main

import (
    "fmt"
    schema "github.com/spatie/schema-org-go"
)

type Business struct {
    *schema.Entity
}

func (business *Business) ToArray() (map[string]any, error) {
    properties, err := business.Entity.ToArray()
    if err != nil {
        return nil, err
    }
    properties["custom"] = true
    return properties, nil
}

func main() {
    business := &Business{Entity: schema.LocalBusiness().Name("Spatie")}
    graph := schema.NewGraph().Set(business)
    graph.LocalBusiness().Email("info@spatie.be")

    data, err := schema.ToJSON(graph)
    if err != nil {
        panic(err)
    }
    fmt.Println(string(data))
}
```

Collections retain the custom `schema.Type`, and generated accessors modify its embedded builder. `Get` and `GetOrCreate` return your original implementation. The same custom document can be assigned to a nested property. There are no process-wide factories or registries to override.

To render a custom type directly, call `schema.ToJSON(business)` or `schema.ToScript(business, "nonce")`. Calling a promoted method such as `business.Entity.ToJSON()` renders only the embedded builder, as usual with Go embedding. If you want `json.Marshal(business)` to use the override, implement `MarshalJSON` on the custom type by returning `schema.ToJSON(business)`.

Custom `ToArray` implementations must return serializable values and handle any cycles they introduce. Builders and collections are mutable. Do not modify them concurrently with rendering. Render to JSON bytes before sharing output between goroutines.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
