0

I want to format a JSON object in my Nodejs server. Delete some fields, rename some fields, and move some fields.

I have many different schemas need to apply to many different JSON, so I hope has a lib that can parse a configuration file and to it.

Maybe a configuration file like this:

DELETE request.logid
DELETE request.data.*.time
MOVE request.data.images data.images

And a JSON before applies an above schema:

{
  "request": {
    "data": {
      "book": {
        "name": "Hello World",
        "time": 1546269044490
      },
      "images": [
        "a-book.jpg"
      ]
    },
    "logid": "a514-afe1f0a2ac02_DCSix"
  }
}

After applied:

{
  "request": {
    "data": {
      "book": {
        "name": "Hello World"
      }
    }
  },
  "data": {
    "images": [
      "a-book.jpg"
    ]
  }
}

Where is the lib it is?

I know that write a function can do the same thing directly, but the problem is I have too many different schemas and too many different JSON, so I wanna manage them by configuration file rather than a js function.

2
  • Can you explain little bit more. If you want to update a json format, write a custom function so that the json can be altered. Commented Dec 31, 2018 at 14:19
  • @Muhammedshah I have updated the question. I know that write a function can do the same thing directly, but the problem is I have too many different schemas and too many different JSON, so I wanna manage them by configuration file rather than a js function. Commented Dec 31, 2018 at 15:20

1 Answer 1

1

Yes you could do something like this...

// Note: psuedocode
// Read the configuration file;
const commands = (await readFile('config')).split('\r\n').split(' ');
// your original JSON;
const obj = {...};
// Modify the JSON given the commands
commands.forEach( row=>{
  if(row[0]==="DELETE"){
    delete obj[row[1]];
  }else if(row[0]==="MOVE"){
    // use lodash to make your life easier. 
    _.set(obj,`${row[2]}`,_.get(obj,`${row[1]}`));
    delete obj[row[1]];
  }else if(...){
    ...
  }
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.