find the extension of a json file in typescript

To find the extension of a JSON file in TypeScript, we can use the built-in Node.js path module, which has a method called extname() that returns the extension of the given file path. Here's an example code block:

index.ts
import * as path from 'path';

const filePath = '/path/to/myfile.json';
const extension = path.extname(filePath);

console.log(extension); // Output: '.json'
158 chars
7 lines

In the above code, we use the path module to get the extension of the myfile.json file, which is .json. Then we log the result to the console.

Note that this code assumes that you have already opened the file (using the fs module, for example) and have the file path stored in a variable called filePath. If you don't have the file path, you'll need to obtain it first before using the path module.

gistlibby LogSnag