Dgo JSON mutation for unicode data in different languages

Is there some way how I can insert data into dgraph using dgo JSON Mutation which contains the same data in different languages?

E.g. the tour of Dgraph shows the following RDF triples:

    _:amit <name> "অমিত"@bn .
    _:amit <name> "Amit"@en .
    _:amit <dgraph.type> "Person" .
    _:amit <age> "35" .
    _:amit <friend> _:michael .
    _:amit <friend> _:sang .
    _:amit <friend> _:artyom .

I can’t think of a way to encode the data into JSON other than creating a struct that looks something like this

type Person struct {
	Uid     string   `json:"uid,omitempty"`
	Name    string   `json:"name@en,omitempty"`
    NameBn  string   `json:"name@bn,omitempty"`
	Age     int      `json:"age,omitempty"`
	Friends []Person `json:"friend,omitempty"`
	OwnsPet []Animal `json:"owns_pet,omitempty"`
	DType   []string `json:"dgraph.type,omitempty"`
}

But this clearly does not scale. How does one do this in general?

You can create a map in Go, fill it with the name in various languages, serialize to JSON and send. Same thing as what Go struct to JSON does, can be achieved via a Go map.

Hi, thanks for the suggestion, but as you probably know yourself that won’t work as the struct must have a “named” (exported) field which will “leak” into the resulting json:

type Person struct {
	Uid   string            `json:"uid,omitempty"`
	Name  map[string]string `json:"name,omitempty"`
	DType []string          `json:"dgraph.type,omitempty"`
}

You can see that in the playground

No, even removing the json tag does not help the situation.

I’m thinking the only way to work around this is to create a custom json.Marshaller, but oh my` I so wanted to avoid doing that, unless there is something I am really missing here.

Well, it’s not pretty, but you could create a map for Name, and marshal it separately from Person. Then, stitch them together by removing the last ‘}’ and first ‘{’ of them.

Told you it would be ugly.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.