Skip to content
This repository was archived by the owner on Sep 4, 2025. It is now read-only.
This repository was archived by the owner on Sep 4, 2025. It is now read-only.

Unmarshal of object/struct attributes fails #74

Open

Description

@gopherfortress

Hello,

I am trying to unmarshal a JSON API response which contains attributes that are objects. I tried marshaling and the result JSON is correct but when I try to unmarshal that same JSON it fails with data is not a jsonapi representation of '*main.Profile'. Here is some example code:

package main

import (
	"bytes"
	"fmt"
	"log"

	"github.com/google/jsonapi"
)

func main() {
	p := &Profile{ID: "1", Avatar: &Avatar{Large: "url1", Small: "url2"}}
	buf := &bytes.Buffer{}
	if err := jsonapi.MarshalOnePayload(buf, p); err != nil {
		log.Fatal(err)
	}

	p = &Profile{}
	fmt.Println(string(buf.Bytes()))
	err := jsonapi.UnmarshalPayload(buf, p)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("ID: %v, Avatar:%v\n", p.ID, p.Avatar)
}

type Profile struct {
	ID     string  `json:"id" jsonapi:"primary,profiles"`
	Avatar *Avatar `json:"avatar" jsonapi:"attr,avatar"`
}

type Avatar struct {
	Large string `json:"large"`
	Small string `json:"small"`
}

I also tried annotating with attr like

type Avatar struct {
	Large string `json:"large" jsonapi:"attr,large,omitempty"`
	Small string `json:"small" jsonapi:"attr,small,omitempty"`
}

but no luck.

Activity

  1. gopherfortress commented on Feb 26, 2017

    @gopherfortress
    Author

    Any news about this issue? Thanks.

  2. aren55555 commented on Aug 1, 2017

    @aren55555
    Contributor

    We have been actively working on this feature in the branch https://github.com/google/jsonapi/tree/feature/embeded-structs

  3. wayne-brush-kw commented on Apr 13, 2018

    @wayne-brush-kw

    Is work on this still progressing? My case is similar but here is my code:

    type Profile struct {
    ID string json:"id" jsonapi:"primary,profiles"
    AvatarList []Avatar json:"avatar" jsonapi:"attr,avatar"
    }

    type Avatar struct {
    Large string json:"large"
    Small string json:"small"
    }

    I tried this with the current embeded-structs branch and it didn't work. Any status on this would be appreciated. The marshal function returned what I expected but the unmarshal returned an error ("data is not a jsonapi representation of '*jsonapi.Profile").

  4. mlitanin commented on Jun 22, 2018

    @mlitanin

    same issue here. Can't unmarshal the json to the struct with attributes that a objects.... Can't even find any workaround.

  5. redmamoth commented on Nov 9, 2022

    @redmamoth

    any update on this issue?

    It seems we can't use arrays of simple types inside attributes either: https://go.dev/play/p/3qufy0gFk3u

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions