Friday, 5 May 2017

How would you add root element to json

I am creating json dynamically

    let schema = fs.readFileSync(`my_schema.json`, {encoding: 'utf8'});
    //need to add root to json schema before save
    result.def =  JSON.parse(schema); 

   fs.writeFile(`my_json.json`, JSON.stringify(result, null, 2));

then when I writing it to the file I've got:

"result.def" = {
  "$schema": "http://json-schema.org/draft-04/schema#",
   "type": "object",
   ...
}

but I need:

"result.def" = {
 "item": {
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  ...
 }
}



via sreginogemoh

No comments:

Post a Comment