admin管理员组文章数量:1404758
I'm trying to reference self-key to in a JSON file in a simple Hello World NodeJS app.
{ "person": { "first_name": "Aminul", "last_name": "Islam" }, "full_name": "{$person.first_name} {$person.last_name}" }
and the app file.
const person = require('./app.json'); console.log(person.full_name);
Expecting result:
Aminul Islam
Result:
{$person.first_name} {$person.last_name}
I'm trying to reference self-key to in a JSON file in a simple Hello World NodeJS app.
{ "person": { "first_name": "Aminul", "last_name": "Islam" }, "full_name": "{$person.first_name} {$person.last_name}" }
and the app file.
const person = require('./app.json'); console.log(person.full_name);
Expecting result:
Aminul Islam
Result:
{$person.first_name} {$person.last_name}Share Improve this question asked Dec 13, 2018 at 9:07 Aminul IslamAminul Islam 831 gold badge3 silver badges8 bronze badges 3
- 1 JSON is a serialized dataformat, it can't contain self or any other references. – Teemu Commented Dec 13, 2018 at 9:11
- stackoverflow./questions/13686161/… – Jaybird Commented Dec 13, 2018 at 9:12
- You can't use js variables inside a JSON, what you can do, is initializing a variable that will contain the full name, then push that as a property to the json. – Zakaria Sahmane Commented Dec 13, 2018 at 9:14
3 Answers
Reset to default 1JSON and Node.js simply don't work like that.
To get that effect you'd need to so something along the lines of:
- Read the raw JSON data using something like
fs.readFile
- Pass the result through a template engine
- Pass the output of the template engine though
JSON.parse
.
it won't work in JSON here is a js workaround
const data = {
"person": {
"first_name": "Aminul",
"last_name": "Islam"
}
}
data["full_name"] = `${data.person.first_name} ${data.person.last_name}`
module.exports = data
and import it
const person = require('./app.js');
console.log(person.full_name);
This is because JSON does not support the use of {$person.first_name}
. It treats it as a string. JSON does no processing for you and is simply a method of holding data.
Your method for reading in the JSON data also appears a little odd. I actually have no idea how that's working for you. The more robust method is as follows:
var fs = require("fs");
var file = fs.readFileSync("./app.json");
var jsonData = JSON.parse(file);
var person = jsonData.person;
console.log(person.first_name + " " + person.last_name);
You already have your data defined no need to expand the contents of your JS file with duplicate data (even if it is in another format).
If you truly need that formatting, generate that data when you create the JSON. If you already have that information being inserted anyway, it's just one more step to add a variable with that formatting.
本文标签: Use JavaScript variables in JSON fileStack Overflow
版权声明:本文标题:Use JavaScript variables in JSON file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744854345a2628686.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论