Standard Library Utilities
JSON Data
JSON helpers convert between strings and typed objects at runtime.
JSON
`JSON.stringify` creates a JSON string, and `JSON.parse` reads a JSON string back into a value.
JSON Data
json.ts
Replay: real traced execution (multi-file project)
type Profile = {
name: string;
points: number;
};
const userName: string = "Ada";
const profile: Profile = { name: userName, points: 42 };
const encoded: string = JSON.stringify(profile);
const decoded: Profile = JSON.parse(encoded) as Profile;
console.log(encoded);
console.log(`${decoded.name}:${decoded.points}`);
type Profile = {
name: string;
points: number;
};
const userName: string = "Grace";
const profile: Profile = { name: userName, points: 42 };
const encoded: string = JSON.stringify(profile);
const decoded: Profile = JSON.parse(encoded) as Profile;
console.log(encoded);
console.log(`${decoded.name}:${decoded.points}`);
type Profile = {
name: string;
points: number;
};
const userName: string = "Linus";
const profile: Profile = { name: userName, points: 42 };
const encoded: string = JSON.stringify(profile);
const decoded: Profile = JSON.parse(encoded) as Profile;
console.log(encoded);
console.log(`${decoded.name}:${decoded.points}`);
userName ← Ada, profile ← [object Object], encoded ← {"name":"Ada","points":42}
3 points: number;4};56const userName→ Ada: string = "Ada"; //@userName="Grace", "Linus"7const profile→ [object Object]: Profile = { name: userNameAda, points: 42 };8const encoded→ {"name":"Ada","points":42}: string = JSON.stringify(profile[object Object]);9const decoded→ [object Object]: Profile = JSON.parse(encoded{"name":"Ada","points":42}) as Profile;1011console.log(encoded{"name":"Ada","points":42});12console.log(`${decoded.nameAda}:${decoded.points42}`);output{"name":"Ada","points":42} Ada:42
userName ← Grace, profile ← [object Object], encoded ← {"name":"Grace","points":42}
3 points: number;4};56const userName→ Grace: string = "Grace";7const profile→ [object Object]: Profile = { name: userNameGrace, points: 42 };8const encoded→ {"name":"Grace","points":42}: string = JSON.stringify(profile[object Object]);9const decoded→ [object Object]: Profile = JSON.parse(encoded{"name":"Grace","points":42}) as Profile;1011console.log(encoded{"name":"Grace","points":42});12console.log(`${decoded.nameGrace}:${decoded.points42}`);output{"name":"Grace","points":42} Grace:42
userName ← Linus, profile ← [object Object], encoded ← {"name":"Linus","points":42}
3 points: number;4};56const userName→ Linus: string = "Linus";7const profile→ [object Object]: Profile = { name: userNameLinus, points: 42 };8const encoded→ {"name":"Linus","points":42}: string = JSON.stringify(profile[object Object]);9const decoded→ [object Object]: Profile = JSON.parse(encoded{"name":"Linus","points":42}) as Profile;1011console.log(encoded{"name":"Linus","points":42});12console.log(`${decoded.nameLinus}:${decoded.points42}`);output{"name":"Linus","points":42} Linus:42