npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

transform_json_keys

v1.0.8

Published

> Made with Bun

Downloads

633

Readme

JSON Key Transformer

Made with Bun

This utility provides a function to transform the keys of a nested JSON object according to a provided schema. It supports complex JSON structures with varying levels of nesting and mixed types.

Features

  • Recursively maps keys of nested objects.
  • Supports arrays and mixed types within the JSON structure.
  • Allows for flexible key transformation using a schema.

Installation

To use this utility, you can install it via npm:

npm install transform_json_keys

or

bun install transform_json_keys

and then

import { transformJsonKeys } from "transform_json_keys";
import type { JsonStructure } from "transform_json_keys";

Fork the Repository

To fork the repository, use the following command:

git clone https://github.com/myapos/transform_json_keys.git

Install Dependencies

To install the necessary dependencies, run:

bun install

Running Tests

To run the tests, use:

bun test

Usage

Example 1: Basic Transformation

import { transformJsonKeys } from "transform_json_keys";
import type { JsonStructure } from "transform_json_keys";

const apiResponse: JsonStructure = {
  user: {
    id: 1,
    name: "John Doe",
    address: {
      street: "123 Main St",
      city: "Anytown",
    },
  },
};

const schema = {
  "user.id": "userId",
  "user.name": "userName",
  "user.address.street": "userStreet",
  "user.address.city": "userCity",
};

const transformedResponse = transformJsonKeys(apiResponse, schema);

console.log(transformedResponse);
// Output:
// {
//   user: {
//     userId: 1,
//     userName: 'John Doe',
//     address: {
//       userStreet: '123 Main St',
//       userCity: 'Anytown'
//     }
//   }
// }

Example 2: Handling Arrays

import { transformJsonKeys } from "transform_json_keys";
import type { JsonStructure } from "transform_json_keys";

const apiResponseWithArray: JsonStructure = {
  users: [
    { id: 1, name: "John Doe" },
    { id: 2, name: "Jane Smith" },
  ],
};

const schemaWithArray = {
  "users.id": "userId",
  "users.name": "userName",
};

const transformedResponseWithArray = transformJsonKeys(
  apiResponseWithArray,
  schemaWithArray
);

console.log(transformedResponseWithArray);
// Output:
// {
//   users: [
//     { userId: 1, userName: 'John Doe' },
//     { userId: 2, userName: 'Jane Smith' }
//   ]
// }

Example 3: Nested Objects

import { transformJsonKeys } from "transform_json_keys";
import type { JsonStructure } from "transform_json_keys";

const nestedApiResponse: JsonStructure = {
  user: {
    profile: {
      firstName: "John",
      lastName: "Doe",
    },
  },
};

const nestedSchema = {
  "user.profile.firstName": "first_name",
  "user.profile.lastName": "last_name",
};

const transformedNestedResponse = transformJsonKeys(
  nestedApiResponse,
  nestedSchema
);

console.log(transformedNestedResponse);
// Output:
// {
//   user: {
//     profile: {
//       first_name: 'John',
//       last_name: 'Doe'
//     }
//   }
// }

Use Cases

1. API Response Transformation

When working with APIs, the response data might not always be in the desired format. This utility can help transform the keys of the JSON response to match the expected format in your application.

2. Data Normalization

In data processing pipelines, it's often necessary to normalize data from different sources. This utility can be used to standardize the keys of JSON objects, making it easier to merge and analyze data.

3. Configuration Management

For applications that rely on configuration files in JSON format, this utility can help transform configuration keys to match the required schema, ensuring consistency across different environments.

4. Logging and Monitoring

When logging JSON data, it's important to have a consistent key format for easier searching and monitoring. This utility can transform the keys of JSON logs to a standardized format.

5. Data Migration

During data migration processes, the structure of JSON data might need to change. This utility can assist in transforming the keys of JSON objects to match the new schema, simplifying the migration process.

6. Frontend State Management

In frontend applications, managing state often involves transforming data received from APIs. This utility can help transform the keys of JSON objects to match the state structure required by the frontend framework.

7. Form Data Handling

When dealing with form submissions, the keys of the JSON data might need to be transformed to match the backend schema. This utility can automate the transformation, reducing the need for manual key mapping.