Home > tough-cookie > Cookie > fromJSON
Does the reverse of Cookie.toJSON().
Signature:
static fromJSON(str: unknown): Cookie | undefined;
Parameter |
Type |
Description |
---|---|---|
str |
unknown |
An unparsed JSON string or a value that has already been parsed as JSON |
Cookie | undefined
Any Date properties (such as .expires, .creation, and .lastAccessed) are parsed via Date.parse, not tough-cookie's parseDate, since ISO timestamps are being handled at this layer.
const json = JSON.stringify({
key: 'alpha',
value: 'beta',
domain: 'example.com',
path: '/foo',
expires: '2038-01-19T03:14:07.000Z',
})
const cookie = Cookie.fromJSON(json)
cookie.key === 'alpha'
cookie.value === 'beta'
cookie.domain === 'example.com'
cookie.path === '/foo'
cookie.expires === new Date(Date.parse('2038-01-19T03:14:07.000Z'))