![](/img/trans.png)
[英]How do you change the name of a root element in JSON with javascript?
[英]How would you add root element to json
我正在動態創建json
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));
然后,當我將其寫入文件時,我得到了:
"result.def" = {
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
...
}
但是我需要:
"result.def" = {
"item": {
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
...
}
}
result.def = { item: JSON.parse(schema) };
要么
result.def.item = JSON.parse(schema);
要么
result = { def: { item: JSON.parse(schema) } };
取決於已經存在的級別以及何時填充它們。
你有沒有嘗試過:
result.def = { "item": JSON.parse(schema) }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.