
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
simple-json-api
Advanced tools
Simple JSON API is a simple, to-the-point library to create JSON API responses that are compliant with the JSON API standard.
Creating a basic JSON API response is very simple:
var JsonAPIResponse = require('simple-json-api');
response.addData('documents')
.id({'AsFaj2K41ks7'})
.attribute({title: 'Report on Organic Farming'})
.attribute({author: 'Timothy B Smith'})
.link({self: 'example.com/docs/AsFaj2K41ks7'});
console.log(response.toJSON());
This code prints out the following JSON API compliant response:
{
data: [
{
type: 'documents',
id: 'AsFaj2K41ks7',
attributes: {
title: 'Report on Organic Farming',
author: 'Timothy B Smith'
}
links: {
self: 'example.com/docs/AsFaj2K41ks7'
}
}
]
}
According to the JSON API standard, a response can have multiple different fields. At the moment this library only supports having data and error objects.
Creating a response is done by simply calling the constructor. To get the JSON representation, call toJSON() on the response object.
var JsonAPIResponse = require('simple-json-api');
let response = new JsonAPIResponse();
console.log(response.toJSON());
{
}
Note that the response object is empty as no data items have been added..
You can add a data item by calling addData(type: String) on the response object. Note that according to the JSON API spec, the type should be a pluralistic noun.
response.addData('users');
console.log(response.toJSON());
{
data: [
{
type: 'users'
}
]
}
The JSON API spec specifies a few fields that a data item can have, but at this time this library only supports id, attributes and links.
Adding an ID is done through the .id(id) function. Repeatedly calling this function overrides the previous ID.
var JsonAPIResponse = require('simple-json-api');
let response = new JsonAPIResponse();
response.addData('users')
.id({'1256789uuid'});
console.log(response.toJSON());
{
data: [
{
type: 'users',
id: '1256789uuid'
}
]
}
Adding attributes is done through the .attribute({key: value}) function. Multiple key-value pairs can be specified in one .attribute() call.
response.addData('users')
.attribute({username: 'Timothy'})
.attribute({password: 'hashedpw'});
console.log(response.toJSON());
{
data: [
{
type: 'users',
attributes: {
username: 'timothy',
password: 'hashedpw'
}
}
]
}
Adding links is done through the .link({key: value}) function. Multiple key-value pairs can be specified in one .link() call.
response.addData('users')
.link({self: 'example.com/users/1256789uuid'});
console.log(response.toJSON());
{
data: [
{
type: 'users',
links: {
self: '1256789uuid'
}
}
]
}
Data items can be appended by repeatedly calling addData() on the response object.
response.addData('users')
.id({'1256789uuid'})
.attribute({username: 'Timothy'})
.attribute({password: 'hashedpw'})
.link({self: 'example.com/users/1256789uuid'});
response.addData('users')
.id({'abcdefguuid'})
.attribute({username: 'Samantha'})
.attribute({password: 'anotherhashedpw'})
.link({self: 'example.com/users/abcdefguuid'});
console.log(response.toJSON());
{
data: [
{
type: 'users',
id: '1256789uuid',
attributes: {
username: 'Timothy',
password: 'hashedpw'
}
links: {
self: 'example.com/users/1256789uuid'
}
},
{
type: 'users',
id: 'abcdefguuid',
attributes: {
username: 'Samantha',
password: 'anotherhashedpw'
}
links: {
self: 'example.com/users/abcdefguuid'
}
}
]
}
Errors can be added by calling addError() on a response object.
response.addError();
Status, title and detailed information can be added through status(), title(), and detail() respectively:
response.addError()
.status('404')
.title('Not Found')
.detail('The resource you are looking for was not available');
This will generate the following response
{
errors: [
{
status: '404',
title: 'Not Found',
detail: 'The resource you are looking for was not available'
}
]
}
As with data items, multiple errors can be added to a single response object.
FAQs
A simple library for creating JSON API responses.
We found that simple-json-api demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.