2

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}
Aminul Islam
  • 75
  • 3
  • 8
  • 1
    JSON is a [serialized](https://developer.mozilla.org/en-US/docs/Glossary/Serialization) dataformat, it can't contain self or any other references. – Teemu Dec 13 '18 at 09:11
  • https://stackoverflow.com/questions/13686161/json-internal-reference-declarations – Jaybird Dec 13 '18 at 09: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 Dec 13 '18 at 09:14

3 Answers3

1

JSON and Node.js simply don't work like that.

To get that effect you'd need to so something along the lines of:

  1. Read the raw JSON data using something like fs.readFile
  2. Pass the result through a template engine
  3. Pass the output of the template engine though JSON.parse.
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

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);
Naor Tedgi
  • 5,204
  • 3
  • 21
  • 48
0

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.

Zitzabis
  • 158
  • 7
  • "Your method for reading in the JSON data also appears a little odd. I actually have no idea how that's working for you." — https://nodejs.org/api/modules.html#modules_require says "Used to import modules, JSON, and local files" – Quentin Dec 13 '18 at 09:22
  • You're quite right. Though I'm still a bit hesitant to handle it that way vs. parsing the data. It also allows for more flexibility beyond just a NodeJS environment if the situation arises. – Zitzabis Dec 13 '18 at 09:24