Library to handle the object that will be sent to an API service
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 :)
- Treat an object with empty, null, array, empty and etc properties
NPM:
npm i handle-json-payload --save
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);
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);
{
teste2: "Some text to represent string",
teste4: [
{
teste6: "Teste 6"
},
{
teste8: "Teste 8"
}
],
teste12: {
teste15: false
},
teste17: false,
teste18: 18
}
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
Found a bug or a problem? Open a new issue GitHub.