I have a JSON file with over 3 million lines in my project and want to write a function that dynamically returns values from that file for a given key. The structure of the JSON is like so:
{
"12345": { // objects are identified by hash keys
"objectProperty1": 12345,
"objectProperty2": "bla",
// ...
},
"67890": {
"objectProperty1": 67890,
"objectProperty2": "blub",
// ...
},
// ...
}
I currently just import the entire file at the top of my code which makes starting the program take several minutes. I'd like to change that somehow.
Then I'd like to know how I can access specifc objects in that file. My function currently looks like this but it always returns undefined
even when I'm sure that the passed key is in the JSON:
import * as jsonFile from './data.json';
const getObjectFromJsonFile = (key: number) => {
const parsedFile = JSON.parse(JSON.stringify(jsonFile));
const keyAsString = key.toString();
return parsedFile[keyAsString];
};
When I call this function with getObjectFromJsonFile(12345)
I get undefined
. What's wrong here?