-1

I am trying to unmarshall JSON-LD using package https://godoc.org/github.com/emersion/go-jsonld

package main

import (

    "fmt"   
    jsonld "github.com/emersion/go-jsonld"
)
func main() {

    text := `{"@context": ["http://schema.org", { "image": { "@id": "schema:image", "@type": "@id"}  }],"id": "http://www.wikidata.org/entity/Q76","type": "Person","name": "Barack Obama","givenName": "Barack","familyName": "Obama","jobTitle": "44th President of the United States","image": "https://commons.wikimedia.org/wiki/File:President_Barack_Obama.jpg"}`
    textBytes := []byte(text)
    var container interface{}
    err := jsonld.Unmarshal(textBytes,container)
    fmt.Println("Error while unmarshalling json-ld: ",err.Error())
    fmt.Println("Output: ",container)
}

Output

Error while unmarshalling json-ld:  jsonld: fetching remote contexts is disabled
Output:  <nil>

I also checked other function for unmarshalling in same package like func UnmarshalWithContext(b []byte, v interface{}, ctx *Context) error but no help.

6
  • UnmarshalWithContext is unrelated to fetching remote data. Commented Oct 15, 2020 at 13:42
  • @Flimzy Any doc where it is mentioned, sir ? Commented Oct 15, 2020 at 13:56
  • Yes. In the doc you linked to: godoc.org/github.com/emersion/go-jsonld#UnmarshalWithContext Commented Oct 15, 2020 at 14:02
  • @Flimzy I referred doc copying line from it "UnmarshalWithContext parses the JSON-LD-encoded data with the context ctx and stores the result in the value pointed to by v." , reason I thought it will fetch remote data because it is having 3rd arg as a context which is a struct and having one of the field as "URL". Hope I made my point sir. Commented Oct 15, 2020 at 14:11
  • The context argument may be relevant to aborting a remote fetch if it cancels, but it won't magically tell the parser how to fetch remote data. Commented Oct 15, 2020 at 14:18

1 Answer 1

1

You have a remote context in the input, so you need either fetch it as in:

package main

import (
    "bytes"
    "fmt"

    jsonld "github.com/emersion/go-jsonld"
)

type person struct {
    ID    string           `jsonld:"@id"`
    Name  string           `jsonld:"name"`
    URL   *jsonld.Resource `jsonld:"url"`
    Image *jsonld.Resource `jsonld:"image"`
}

func main() {

    text := `{"@context": ["http://schema.org", { "image": { "@id": "schema:image", "@type": "@id"}  }],"id": "http://www.wikidata.org/entity/Q76","type": "Person","name": "Barack Obama","givenName": "Barack","familyName": "Obama","jobTitle": "44th President of the United States","image": "https://commons.wikimedia.org/wiki/File:President_Barack_Obama.jpg"}`
    textBytes := []byte(text)
    var container person
    dec := jsonld.NewDecoder(bytes.NewReader(textBytes))
    dec.FetchContext = func(url string) (*jsonld.Context, error) {
        var fetchedContext jsonld.Context //TODO fetch the context
        return &fetchedContext, nil
    }

    err := dec.Decode(&container)
    fmt.Println("Error while unmarshalling json-ld: ", err)
    fmt.Println("Output: ", container)
}

Or provide the schema in the input. You can refer to tests as examples of input and output.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.