This is my environment variable:
export DATA='{firstName: "OAMAR", lastName: "KANJI"}'
process.env.DATA sees this as a string but doing something like JSON.parse(process.env.DATA) does not work as the keys in the object are not strings. I.e something like JSON.parse('{"firstName": "OAMAR", "lastName": "KANJI"}') would work but this is not the form of the environment variable.
Any ideas on how to convert the string to JSON?
asked Jan 25, 2019 at 20:39
Oamar Kanji
2,2747 gold badges29 silver badges49 bronze badges
2 Answers 2
export DATA='{"firstName": "OAMAR", "lastName": "KANJI"}'
change your format then used like
var foo =JSON.parse(DATA);
answered Jan 25, 2019 at 21:15
Abdullah Khan
6455 silver badges11 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You can try converting your string to a valid JSON String then change it back to JSON
const Data ='{firstName: "OAMAR", lastName: "KANJI"}';
const output = JSON.parse(JSON.stringify(Data));
console.log(output);
answered Jan 25, 2019 at 21:35
Melchia
24.6k23 gold badges110 silver badges130 bronze badges
Comments
Explore related questions
See similar questions with these tags.
lang-js
'OAMAR'is wrong as well.eval(), but that's dangerous.export DATA='{"firstName": "OAMAR", "lastName": "KANJI"}'should not cause an error.