Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8491131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:19:32+00:00 2026-06-10T22:19:32+00:00

Say I am building an API with a call that returns a collection of

  • 0

Say I am building an API with a call that returns a collection of cities, each of which has a relationship to a state. A state has many cities, but a city has only one state.

I can imagine flattening the relationship and obscuring the underlying structure of the data like this,

{ cities : [
    { id: 1,
      name: "Los Angeles",
      state: "CA" }
]}

Or I can imagine structuring the JSON such that the relationship between cities and states is apparent,

{ cities : [
    { id: 1,
      name: "Los Angeles",
      state: { id: 1,
               name: "CA" }
]}

The consumer of the API, as of now, only ever needs to know the name of the state. He does not need to know its ID or a way to get more information about the state. What are the pros and cons in structuring the JSON in either way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T22:19:34+00:00Added an answer on June 10, 2026 at 10:19 pm

    That depends on the other consumers. Do you have any? Are you planning to?

    An API is a machine interface, it is equally easy for the consumer’s developer to use both structures. If the “state” entity is not a compound entity(no usable properties except name), it might me a good idea to show just the name, not a structure with the id.

    If there is a possibility that state id might be useful in future, or a possibility that a new property is going to be added to state entity, then you should use the second approach from the start. Changing API in any way breaks the software already written, so changing it will make you support two different versions. Change between approaches 1 and 2 is not backward-compatible.

    I would go with approach 2. It is not much more complex than 1 and leaves a possibility to extend state entity.

    There is also a third approach. But it is noticeably more complex(and more extendable). Return state id only and create a method for state entity retrieval.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a class that interacts with an API. Say the API has a
Say I'm building a UDF class called StaticLookupUDF that has to load some static
Let's say we are building some public service that grabs the setup of a
We are building web applications for the iPhone that work offline. But we are
I am building an application that would record what people say, generate an audio
Let's say I am building an web app that is connecting and basically parsing
I am building an application that has a web front end and a desktop
Say I'm building an ASP.Net class that inherits from IHttpHandler , should I wire
I'm building a buffer in Java that will collect write actions requested by many
Let's say you are building a multilingual web application in which all interface text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.