All Projects → donavon → json_

donavon / json_

Licence: MIT license
Converts camelCase JavaScript objects to JSON snake_case and vise versa.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to json

fetch-reject
Simple wrapper for fetch which rejects on HTTP error
Stars: ✭ 70 (+337.5%)
Mutual labels:  fetch
sysfetch
A super tiny system information fetch script written in BASH
Stars: ✭ 197 (+1131.25%)
Mutual labels:  fetch
beccaccino
Beccaccino is an easy, sexy, reliable, framework agnostic http client for redux that is ⚡️beccaccino fast!
Stars: ✭ 13 (-18.75%)
Mutual labels:  fetch
comlink-fetch
⚙️ A Web worker fetch wrapper using comlink
Stars: ✭ 43 (+168.75%)
Mutual labels:  fetch
gojax
A set of extensions for goja javascript engine
Stars: ✭ 24 (+50%)
Mutual labels:  fetch
i18next-http-backend
i18next-http-backend is a backend layer for i18next using in Node.js, in the browser and for Deno.
Stars: ✭ 270 (+1587.5%)
Mutual labels:  fetch
rxrest
Reactive rest library
Stars: ✭ 33 (+106.25%)
Mutual labels:  fetch
async-preloader
Assets preloader using ES2017 async/await and fetch.
Stars: ✭ 44 (+175%)
Mutual labels:  fetch
miniprogram-network
Redefine the Network API of Wechat MiniProgram (小程序网络库)
Stars: ✭ 93 (+481.25%)
Mutual labels:  fetch
react-express-mongodb
基于react全家桶+antd design+webpack2+node+express+mongodb开发的前后台博客系统
Stars: ✭ 26 (+62.5%)
Mutual labels:  fetch
micro-cacheable
A micro utility for data caching
Stars: ✭ 35 (+118.75%)
Mutual labels:  fetch
r-music
react music demo
Stars: ✭ 57 (+256.25%)
Mutual labels:  fetch
gotify-push
Chrome Extension for Send Push Notification 🔔 to gotify/server ☁
Stars: ✭ 32 (+100%)
Mutual labels:  fetch
vue2-element
基于vue2 + vue-router2 + element-ui + vuex2 + fetch + webpack2 企业级后台管理系统最佳实践
Stars: ✭ 115 (+618.75%)
Mutual labels:  fetch
xhttp
Tiny shortcuts for using the native fetch API. Provides a fluent builder-style API for request building and response reading.
Stars: ✭ 31 (+93.75%)
Mutual labels:  fetch
react-native-fetch
🌍Fetch API wrapped as a component with support for retries & timeouts
Stars: ✭ 20 (+25%)
Mutual labels:  fetch
react-sync
A declarative approach to fetching data via a React higher order component
Stars: ✭ 18 (+12.5%)
Mutual labels:  fetch
fetch
A fetch API polyfill for React Native with text streaming support.
Stars: ✭ 27 (+68.75%)
Mutual labels:  fetch
parcel-plugin-goodie-bag
provides the Promise and fetch goodies needed for IE(11) support w/ parcel bundle loading
Stars: ✭ 15 (-6.25%)
Mutual labels:  fetch
sysfex
Another system information fetching tool written in C++
Stars: ✭ 107 (+568.75%)
Mutual labels:  fetch

json_

Build Status npm version

Converts camelCase JavaScript objects to JSON snake_case and vise versa. This is a direct replacement for the built-in JSON object. In fact, this simply wraps the built-in JSON object. Very handy when your back_end APIs are not build using Node.js. It also supports converting a fetch response stream into a camelCased object.

NOTE: New version 3.0 completely re-written in TypeScript, so it's fully typed.

First, get the package

Install json_ and include it in your build.

npm install json_ --save

Then import it like this.

import JSON_ from 'json_';

Example

const example = {
  firstName: 'John',
  lastName: 'Doe',
  isbn10: '1234567890',
};

console.log(JSON_.stringify(example));
// {"first_name":"John","last_name":"Doe", "isbn_10": "1234567890"}

And vise versa.

import JSON_ from 'json_';
const str = '{"ultimate_answer": 42}';

console.log(JSON_.parse(str));
// {ultimateAnswer: 42}

Using with fetch

You can use json_ directly with the JavaScript fetch API to convert the Response into an Object with snakeCase.

Let's say you have a function that returns snake_case weather data, something like this.

const fetchWeather = async zip => {
  const response = fetch(`${weatherUrl}?zip=${zip}`);
  const json = await response.json();
  return json;
};

const data = await fetchWeather('10285');
console.log(data);
// {current_temp: 85, reporting_station: 'New York, NY'}

You can easily convert the resolved object to camelCase by replacing the call to Response.json() to a call to JSON_.parse(Response), like this.

- const json = await response.json();
+ const json = await JSON_.parse(response);

The resulting code looks like this

import JSON_ from 'json_';

const fetchWeather = async zip => {
  const response = fetch(`${weatherUrl}?zip=${zip}`);
  const json = await JSON_.parse(response);
  return json;
};

const data = await fetchWeather('10285');
console.log(data);
// {currentTemp: 85, reportingStation: 'New York, NY'}

Tests!

100% unit test coverage. To run the unit tests and get a coverage report:

npm test coverage

License

Copyright Donavon West. Released under MIT license

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].