Jeremy P. Beasley
Jeremy P. Beasley

Reputation: 709

How to build One-To-Many relationships within JSON API

I`m very new to JS/APIs/JSON.

I`ve got an API I built using deployd, a great tool that allows to to quickly set up an API.

My API called "app" has two resources zips and people. Each entry/object in zips has only one property zipcode. Each people has the properties name and phonenumber.

I want to figure out a way to have people be associated with various zipcodes. Either one or many if necessary. This would, as I understand, involve adding a property to people such as assoczipcodes and using a relationship model to indicate with zipcodes are related.

I've done some research here about how a relationship is structured but I simply don't understand the syntax and format.

Q: What datatype is correct for associzipcodes?

Q: What do I enter into each assoczipcodes to indicate which zips are related?

Upvotes: 2

Views: 1545

Answers (2)

YWCA Hello
YWCA Hello

Reputation: 3059

Cristik's answer appears to be outdated. According to the current documentation, you'd want to nest the relationship like this:

{
    "type": "people",
    "id": "1",
    "attributes": {
       "name": "John Doe"
    },
    "relationships": {
        "zipcodes": {
            "links": {
                "self": "http://myserver.com/people/1/zipcodes"
            }
        }
    }
}

Upvotes: 3

Cristik
Cristik

Reputation: 32870

Based on the contents from the Resource Relationships section, the simplest approach would be to return an URL for the relationship, and have that URL return all information needed:

{
    "type": "people",
    "id": "1",
    "name": "John Doe",
    "links": {
        "zipcodes": "http://myserver.com/people/1/zipcodes"
    }
}

Upvotes: 0

Related Questions