handle-json-payload
TypeScript icon, indicating that this package has built-in type declarations

1.0.5 • Public • Published

handle-json-payload

Library to handle the object that will be sent to an API service

GitHub top language GitHub code size in bytes GitHub issues GitHub closed issues GitHub contributors GitHub npm Website

handle-json-payload I'm making this library to solve a problem with payload objects, the purpose of this library is to intercept the final object, clean, fix, remove null properties, empty arrays, undefined. I hope to be able to contribute to the community! Welcome to add more features. Thanks :)

Characteristics

  • Treat an object with empty, null, array, empty and etc properties

How to use

Installation

NPM:

npm i handle-json-payload --save

1 - Simple mode, using object handling

How to use without intercepting the data

const payload = require('handle-json-payload');

const yourObject = {
    "teste1": null,
    "teste2": "Some text to represent string",
    "teste3": [],
    "teste4": [
        {
            "teste5": null
        },
        {
            "teste6": "Teste 6"
        },
        {
            "teste7": [],
            "teste8": "Teste 8",
            "teste9": [
                {
                    "teste10": null,
                    "teste11": []
                }
            ]
        }
    ],
    "teste12": {
        "teste13": null,
        "teste14": {},
        "teste15": false
    },
    "teste16": {},
    "teste17": false,
    "teste18": 18,
    "teste19": [
        {},
        {
            "teste20": []
        }
    ]
}

const cleanObject = payload.treatObjectToSend(yourObject);
console.log(cleanObject);

2 - Now how to intercept and handle the traversed values

If you want to intercept the values ​​and treat you can follow this second example

const payload = require('handle-json-payload');

const yourObject = {
    "teste1": null,
    "teste2": "Some text to represent string",
    "teste3": [],
    "teste4": [
        {
            "teste5": null
        },
        {
            "teste6": "Teste 6"
        },
        {
            "teste7": [],
            "teste8": "Teste 8",
            "teste9": [
                {
                    "teste10": null,
                    "teste11": []
                }
            ]
        }
    ],
    "teste12": {
        "teste13": null,
        "teste14": {},
        "teste15": false
    },
    "teste16": {},
    "teste17": false,
    "teste18": 18,
    "teste19": [
        {},
        {
            "teste20": []
        }
    ]
}

This function (callback) will be called on all values

const callback = (data) => {
    console.log('Data->', data);
    return data;
}

const cleanObject = payload.treatObjectToSend(yourObject, callback);
console.log(cleanObject);

FINAL RESULT


  {
    teste2: "Some text to represent string",
    teste4: [
        {
            teste6: "Teste 6"
        },
        {
            teste8: "Teste 8"
        }
    ],
    teste12: {
        teste15: false
    },
    teste17: false,
    teste18: 18
  }

Solutions and Errors

1 - For those with TypeScript getting error TS2304: can't find the name 'requires'

2 - TypeScript getting error, cannot find name 'require' - TypeScript 1.x

Help improve

Found a bug or a problem? Open a new issue GitHub.

Contributing to this project

GitHub

Autor

@JeanPauljeanpaulwebb@gmail.com.br

GitHub contributors

Readme

Keywords

Package Sidebar

Install

npm i handle-json-payload

Weekly Downloads

10

Version

1.0.5

License

MIT

Unpacked Size

1.77 MB

Total Files

11

Last publish

Collaborators

  • jeanpaull