1

I have a large JSON file that is generated by another process that I can't modify. In case of blank values, that process puts "null" as a value of those properties.

And I need to treat these "null" values as null in my Nodejs server side, or else they are taken as literal string. I would like to see if there is a way with JSON.parser(fs.readFileSync('...')) to read the file and create a local variable that has null instead of "null" for those properties.

NuCradle
  • 665
  • 1
  • 9
  • 31
  • 2
    Why not fix the generation and make it behave correctly? People with the last name "Null" already have enough problems and don't need another application treating them as if they don't exist. Or a user typing "null" intentionally breaking an application. Or any other number of things that result in "null" as a legitimate string that will then cause obscure hard to track down and harder to fix bugs to crop up. – VLAZ Sep 12 '19 at 16:36
  • As I said, I don't have access to it. Besides, it's going to affect many other components. – NuCradle Sep 12 '19 at 17:20

2 Answers2

0

The correct way of achieving this is using the reviver function as per the JSON.parse() documentation:

let string = '{"key1": "value1", "key2": "null"}';

let json = JSON.parse(string, function reviver(key, value) {
  return (value == 'null') ? null : value;
});

console.log(json);
Renan
  • 1,705
  • 2
  • 15
  • 32
-1

Check this answer.

Instead of replacing it with text, replace 'null' with null.

so:

// test.json content:
{
    "test": "null",
    "test2": "null",
    "test3": null,
    "test4": [1, null, "null", 2, "3"]
}


// index.js content:
const fs = require('fs');

const source = fs.readFileSync('test.json', 'utf8');
const fixedJson = source.replace(/"null"/g, 'null');
const fixedObject = JSON.parse(fixedJson);

console.log(fixedJson);
console.log(fixedObject);
console.log(fixedObject.test3 === null);
yeya
  • 1,968
  • 1
  • 21
  • 31
  • 1
    That doesn't seem to work. I still get `"null"` value back. – NuCradle Sep 12 '19 at 17:20
  • No, you will get `null`, not `'null'`. That what you asked for. What else do you want to get? – yeya Sep 12 '19 at 18:07
  • You ask for `null` in Node variable. In case you want to get `''` in the variable, change the second argument of the replace to `''`. – yeya Sep 12 '19 at 18:14
  • When I say `null`, the outcome should be like `{ "test": null}`. I want to be able to iterate through the properties and match against null, e.g. `if (test === null)`. – NuCradle Sep 12 '19 at 18:23
  • Great, so please accept my answer, because `fixedObject.test === null` will result to `true`. Thanks in advance – yeya Sep 12 '19 at 18:36
  • Have you tested this to see if you can read the JSON from a fall (as the description reads), replace those null values, then parse it back to that same variable, then use it for testing? I don't seem to be able to get this working (Nodejs server side). – NuCradle Sep 12 '19 at 19:32
  • Did you test it? Anyway, please post the file content for example. – yeya Sep 12 '19 at 20:06
  • The old replace function worked only for the first occurrence, check the fixed answer. Be aware @vlaz comment. If any key is "null" you will get an error at `JSON.parse` – yeya Sep 23 '19 at 15:59