Ja serializer response format

I am using JA-serializer library for api data serialization and below are my Phoenix views configured according to JA-serializer.

defmodule CheetahApiWeb.UserView do
  use CheetahApiWeb, :view
  use JaSerializer.PhoenixView

  attributes [
    :id, :name, :email_address, :mobile_number
  ]

    has_one :contact_detail, include: true, serializer: CheetahApiWeb.ContactDetailView
    has_one :work_experience, include: true, serializer: CheetahApiWeb.WorkExperienceView
  
end

defmodule CheetahApiWeb.ContactDetailView do
  use CheetahApiWeb, :view
  use JaSerializer.PhoenixView

  attributes [
    :id, :primary_email, :secondary_email
  ]

end

defmodule CheetahApiWeb.WorkExperienceView do
  use CheetahApiWeb, :view
  use JaSerializer.PhoenixView

  attributes [
    :id, :professional_headline, :total_years_experience
  ]

    has_many :work_details, include: true, serializer: CheetahApiWeb.WorkDetailView
  
end


defmodule CheetahApiWeb.WorkDetailView do
  use CheetahApiWeb, :view
  use JaSerializer.PhoenixView

  attributes [
    :id, :description, :start_date
  ]

end

And this is how the response I am getting with above configuartions.

{
    "jsonapi": {
        "version": "1.0"
    },
    "included": [
        {
            "type": "contactDetail",
            "id": "1",
            "attributes": {
                "secondaryEmail": "shekharindian099@gmail.com",
                "primaryEmail": "shekharait254@gmail.com",
                "id": 1
            }
        },
        {
            "type": "workExperience",
            "relationships": {
                "workDetails": {
                    "data": [
                        {
                            "type": "workDetail",
                            "id": "3"
                        },
                        {
                            "type": "workDetail",
                            "id": "2"
                        }
                    ]
                }
            },
            "id": "1",
            "attributes": {
                "totalYearsExperience": 5,
                "professionalHeadline": null,
                "id": 1
            }
        },
        {
            "type": "workDetail",
            "id": "2",
            "attributes": {
                "startDate": "2016-04-25",
                "id": 2,
                "description": null
            }
        },
        {
            "type": "workDetail",
            "id": "3",
            "attributes": {
                "startDate": "2016-04-25",
                "id": 3,
                "description": null
            }
        }
    ],
    "data": {
        "type": "user",
        "relationships": {
            "workExperience": {
                "data": {
                    "type": "workExperience",
                    "id": "1"
                }
            },
            "contactDetail": {
                "data": {
                    "type": "contactDetail",
                    "id": "1"
                }
            }
        },
        "id": "1",
        "attributes": {
            "name": "Chandra Shekhar",
            "mobileNumber": "7875052320",
            "id": 1,
            "emailAddress": "shekharait254@gmail.com"
        }
    }
}

Someone who has used JA-searialzer library before can verify if this is the response which i will get everytime or I can configure it something else , because I want the response in this simple format.

{
  "data": {
    "id": 1,
    "name": "Chandra Shekhar",
    "mobileNumber": "7875052320",
    "emailAddress": "shekharait254@gmail.com",
    "contact_detail": {
      "id": 1      
      "secondaryEmail": "shekharindian099@gmail.com",
      "primaryEmail": "shekharait254@gmail.com",
    },
    "work_experience": {
      "id": 1
      "totalYearsExperience": 5,
      "professionalHeadline": null,
      "work_details": [
        {
        "startDate": "2016-04-25",
        "id": 2,
        "description": null
        },
        {
          "startDate": "2016-04-25",
          "id": 3,
          "description": null
        }
      ]
    }
  }
}

Is there are some configurations which i need to change so that I can get the above response as I am expecting.

I think you’re confused about JaSerializer and what it does.

JaSerializer serialises your data into JSON API formatted output. JSON API is a really specific and not generic (take a look at http://jsonapi.org/). What you’re asking to do is to completely ignore the JSON API spec… So you basically don’t need to use JaSerializer. You can just use the built in Phoenix view stuff to format your data as you want. The Phoenix Guide on JSON rendering might help you.

3 Likes

Yes I was confused before, now I got it, thanks for the update.