Skip to content

Instantly share code, notes, and snippets.

@kharysharpe
Last active May 19, 2017 14:31
Show Gist options
  • Save kharysharpe/81ade8868728a6779d02c3ca290ab5c9 to your computer and use it in GitHub Desktop.
Save kharysharpe/81ade8868728a6779d02c3ca290ab5c9 to your computer and use it in GitHub Desktop.
JSON API Response

Schema:


{
	success: true|false
	container: {}|[],
	meta: {
		page: [int],
		perPage: [int],
		totalPages: [int]
	},
	error: {
		code: (alpha)
		description: (alpha),
		messages: [
			key: []
		],
		meta: {}
	}
}

Key Data Type Description
success Boolean True is the request was successful and False otherwise. When false a error object is expected
container Object or Array Holds the result/output of a successful request.
meta Object Stores pagination information or any additional information that describes the container's result
error Object Holds the error code, description and all the related messages

Example #1


{
  success: true
  container: {
    id: 1,
    name: 'Baby Doe',
    age: 1
  },
  meta: {
    page: 1,
    perPage: 10,
    totalPages: 1
  }
}

Example #2


{
  success: true
  container: [{
    id: 1,
    name: 'John Doe',
    age: 30
  }, {
    id: 2,
    name: 'Jane Doe',
    age: 25
  }],
  meta: {
    page: 1,
    perPage: 10,
    totalPages: 1
  }
}

Example #3


{
  success: false,
  error: {
    code: 400,
    description: 'Invalid input',
    messages: [
    	firstname: [
	  'First name is required',
	  'First name must have at least two characters
	],
    	lastname: [
	  'Last name cannot be left empty',
	  'Last name must have at least two characters
	],
    ]
  }
}  
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment