I am trying to display the value of a key after using JSON.stringify to collect form data and put into JSON. Now I need the value of a specific key within from this json file but I get the key/value pair:
{"KEY":"KM8IJM12D56U303366"}
Currently my code to display this is:
const onSubmit = async data => {
// How to get specific data from stringify
var jsonString = JSON.stringify(data, ["KEY"]);
alert(jsonString);
notify();
};
I have tried using getItem() but that seems to only work with local storage. Is there a solution where I can just directly access the value of KEY after JSON.stringify()?
Amigo, My first suggestion over this would be, Go with the data as Object, don't stringify that.
const onSubmit = async data => {
// If data is not a Stringify Object:
var jsonString = JSON.stringify(data.key);
alert(jsonString);
notify();
//********
// If data is a Stringify Object:
var jsonString = JSON.parse(data);
alert(jsonString.key);
notify();
};
But If you want to go with a string:
const onSubmit = async data => {
// If you want to use only the Key as a string, go with the line below
var jsonString = data.key.toString();
// Else Directly access your key by the below line.
var jsonString = data.key;
alert(jsonString);
notify();
};
You would need to JSON.parse() it back to an object to access keys and values. After you apply stringify(), it's a string.