json-forgefy
v1.1.0
Published
Creates a new JSON Object, based on a blueprint object with params.
Downloads
6
Readme
JSON-FORGEFY
Ideia:
The idea of this package is to provide a way to transform an upcoming JSON string into a new Object, following a blueprint configuration file.
This came with the necessity of consuming 3rd services API where you can setup a JSON Schema Like configuration file to obtain the desired JSON structure as response, before sending it forward to the next downstream service.
The purpose of this package is to make it heavily agnostic of the system. You don't have to bother parsing each prop from A to B, just provide the blueprint and let the package do the rest.
This is heavily inspired on the MongoDB aggregation pipeline, where you can transform the data as you wish. I'll try to add as much operators as needed, following their official documentation.
Example:
export const incomingPayload = {
transactionId: "1234567890",
transactionType: "Money Transfer",
transactionDate: "2024-01-01T00:00:00Z",
transactionStatus: "Pending",
sender: {
accountId: "A123",
accountType: "Savings",
bank: {
bankId: "B123",
bankName: "Bank A",
branchName: "Branch A",
ifscCode: "IFSC123",
swiftCode: "SWIFT123",
address: {
street: "Street A",
city: "City A",
state: "State A",
country: "Country A",
zipCode: "ZIP123",
},
},
},
receiver: {
accountId: "B456",
accountType: "Checking",
bank: {
bankId: "B456",
bankName: "Bank B",
branchName: "Branch B",
ifscCode: "IFSC456",
swiftCode: "SWIFT456",
address: {
street: "Street B",
city: "City B",
state: "State B",
country: "Country B",
zipCode: "ZIP456",
},
},
},
transactionDetails: {
amount: "1000.0",
currency: "USD",
exchangeRate: "1.0",
transactionFee: "10.0",
netAmount: "990.0",
description: "Money Transfer to Account B456",
},
};
To transform our incoming JSON string into a new Object, we can create a blueprint configuration file like this one below:
export const yourBlueprint = {
accountId: "$sender.accountId",
accountType: "$sender.accountType",
transactionId: "$transactionId",
transactionStatus: { $toUpper: "$transactionStatus" },
transactionDetails: {
amount: {
$multiply: [{ $toNumber: "$transactionDetails.amount" }, 100],
},
currency: "$transactionDetails.currency",
fees: {
transactionFee: "$transactionDetails.transactionFee",
netAmount: "$transactionDetails.netAmount",
},
},
receiverType: {
$cond: {
if: { $eq: ["$receiver.accountType", "Preparing"] },
then: "It is checking...",
else: "Finished",
},
},
branch: {
$switch: {
branches: [
{
case: { $eq: ["$sender.bank.branchName", "Branch A"] },
then: "Open Banking Name A",
},
{
case: { $eq: ["$sender.bank.branchName", "Branch B"] },
then: "Open Banking Name B",
},
],
default: "Unknown Branch",
},
},
};
The end result will be:
export const expectedResult = {
accountId: "A123",
accountType: "Savings",
transactionId: "1234567890",
transactionStatus: "PENDING",
transactionDetails: {
amount: 100000,
currency: "USD",
fees: { transactionFee: "10.0", netAmount: "990.0" },
},
receiverType: "Finished",
branch: "Open Banking Name A",
};
How to:
Import the forgefy
function from the package and pass two inputs, the incoming JSON string and the blueprint configuration file.
import { forgefy } from "json-forgefy";
const result = forgefy(incomingPayload, yourBlueprint);