Unmarshal JSON into struct



I am not sure if this is a Cayley question or unmarshal of JSON into struct kind of question. I am trying to load a json file with a clinic information that includes hours of operation into a Go struct and I am not sure how do do that.

I have the following structs:

type Clinic struct {
	Name      string         `json:"name" quad:"name"`
	Address1  string         `json:"address" quad:"address"`
	CreatedBy quad.IRI       `quad:"createdBy"`
	Hours     []OpeningHours `quad:"schema:openingHoursSpecification"`

type OpeningHours struct {
	DayOfWeek quad.IRI `json:"day" quad:"schema:dayOfWeek"` // set to one of consts like the one above
	Slot      int      `json:"slot" quad:"slot"`
	Opens     string   `json:"opens" quad:"schema:opens"` // ex: 12:00 or 12:00:00
	Closes    string   `json:"closes" quad:"schema:closes"`

This is the json file that represent a clinic and the hours of operation:

  "name": "Heal Now",
  "Address": "3234 Rot Road, Singapore",
  "CreatedBy": "Doe Joe",
  "hours": {
    "mon": [
      {"1": {"from": "08:00", "to": "12:00"}},
      {"2": {"from": "13:00", "to": "15:30"}},
      {"3": {"from": "16:00", "to": "19:00"}}
    "tue": [
      {"1": {"from": "09:00", "to": "12:30"}},
      {"2": {"from": "13:00", "to": "18:00"}}

This is the function I use to unmarshal the json file into a Go struct:

func loadJSON(JSONFile string) *Clinic {
	raw, err := ioutil.ReadFile(JSONFile)

	if err != nil {

	var c Clinic

	err = json.Unmarshal(raw, &c)

	if err != nil {

	return &c

If anyone want to reproduce it run go run main.go - https://github.com/oren/cayley-docs/tree/288c2e61df2c6f371e76833615eb9d0f0767ff67/how-to-guides/04-insert-using-json



Hi Oren, seems to me like your json structure and data does not match with your struct.

This seems to do what you want.

  "name": "Heal Now",
  "Address": "3234 Rot Road, Singapore",
  "CreatedBy": "Doe Joe",
  "hours": [
      {"day":"mon", "slot":1, "opens": "08:00", "closes": "12:00"},
      {"day":"mon", "slot":2, "opens": "13:00", "closes": "15:30"},
      {"day":"mon", "slot":3, "opens": "16:00", "closes": "19:00"},
      {"day":"tue", "slot":1, "opens": "09:00", "closes": "12:30"},
      {"day":"tue", "slot":2, "opens": "13:00", "closes": "18:00"}
<d4bfb5d8-ef0e-11e7-ac90-c8e0eb18535f> -- <rdf:type> -> <schema:OpeningHoursSpecification>
<d4bfb5d8-ef0e-11e7-ac90-c8e0eb18535f> -- <schema:dayOfWeek> -> <tue>
<d4bfb5d8-ef0e-11e7-ac90-c8e0eb18535f> -- <slot> -> "2"^^<schema:Integer>
<d4bfb5d8-ef0e-11e7-ac90-c8e0eb18535f> -- <schema:opens> -> "13:00"
<d4bfb5d8-ef0e-11e7-ac90-c8e0eb18535f> -- <schema:closes> -> "18:00"
<d4bfb420-ef0e-11e7-ac90-c8e0eb18535f> -- <schema:openingHoursSpecification> -> <d4bfb5d8-ef0e-11e7-ac90-c8e0eb18535f>


Well done Jorgen, beat me to it!
Oren, I was also going to suggest you check that the names of the “Clinic” fields/json/quads match exactly.
A JSON issue, rather than a Cayley one methinks.


Thank you both!


works as expected.

I am not sure if it matters but I believe it stores the day as a string -
<schema:dayOfWeek> -> <mon>

should I try storing it as a more semantic day like this? if so, how do i do that?
<schema:dayOfWeek> -> <http://schema.org/Monday>


Actualy, why would it even work without the json annotation (or whatever the name for this) in the struct?

shouldn’t this
Hours []OpeningHoursquad:“schema:openingHoursSpecification”be `Hours []OpeningHours `json: "hours" quad:"schema:openingHoursSpecification"


It will work, but the field name in json will be “Hours”.