3

I am currently trying to reference a JSON path and store it in a constant. I tried making a reference with a string and I res.send it as a response and it gave me back my string. So what would be the most adapted way to proceed to reference a JSON file in order to for example query fields of it?

Thank you for your help!

enter image description here

4
  • Simply importing the file with nodejs will automatically convert it to an object. See the nodejs docs, specifically step 3 of LOAD_AS_FILE(X). Commented Aug 5, 2018 at 3:39
  • I tried the LOAD_AS_FILE(X) but it actually doesn't let me store in a constant does it?... Commented Aug 5, 2018 at 3:54
  • Possible duplicate of Typescript compiler error when importing json file Commented Aug 5, 2018 at 13:30
  • It appears there was some confusion. LOAD_AS_FILE(X) is not an actual method but is technical writing describing how the module loading algo works in NodeJS. The point was to illuminate that JSON can be directly loaded in NodeJS without the need to manually read a file and parse the JSON to access the data represented by it. Commented Aug 5, 2018 at 13:45

2 Answers 2

0

Since you have the contents of the JSON file loaded into the string already, you must just remember to set the Content-Type header on the response to application/json. This is what indicates to the client what the type of the response body is. Assuming you are using express, you just need to add res.setHeader('Content-Type', 'application/json'); before your call to res.send.

Sign up to request clarification or add additional context in comments.

2 Comments

Actually it seems like the content isn't loaded at all.. It just seems like a string containing my path. I just print the content of it or even res.json it it just returns me './keyfile.json'. Not the actual content of the JSON file. I am a bit lost in terms of syntax here. Seems like a hell just to reference a file and query its content...
I see... Sorry, I misread your question regarding the 'query fields of it' part... I believe that Jason Cust's answer should solve your issue then and hopefully mine will be of some use once you get the JSON loaded :)
0

Within nodejs if a JSON file is simply imported then it will automatically be parsed into an object:

const keyFile = require('./keyfile.json');

Update: If you are using TS >=2.9:

Support for well-typed JSON imports TypeScript is now able to import JSON files as input files when using the node strategy for moduleResolution. This means you can use json files as part of their project, and they’ll be well-typed!

// ./tsconfig.json
{
    "compilerOptions": {
        "module": "commonjs",
        "resolveJsonModule": true,
        "esModuleInterop": true
        "outDir": "lib"
    },
    "include": ["src"]
}

// ./src/settings.json
{
    "dry": false,
    "debug": false
}

// ./src/foo.ts
import settings from "./settings.json";
settings.debug === true;  // Okay
settings.dry === 2;       // Error! Can't compare a `boolean` and `number`

These JSON files will also carry over to your output directory so that things “just work” at runtime.

3 Comments

JSON file is not recognized as a module in TS as it indicates me in the error when I try the import. Can't I just simply reference a file in TypeScript and store it in a variable???!
@JulienHora I checked the docs directly and the answer is quoted directly from there. Hope that helps.
Just to clarify for anyone else who was confused like me: even with the new Typescript behavior, it still only imports the json file as a string, whereas if you use "require" it also converts the string to a JS object

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.