tough-cookie.cookie.fromjson.md

July 9, 2025 ยท View on GitHub

Home > tough-cookie > Cookie > fromJSON

Cookie.fromJSON() method

Does the reverse of Cookie.toJSON().

Signature:

static fromJSON(str: unknown): Cookie | undefined;

Parameters

Parameter

Type

Description

str

unknown

An unparsed JSON string or a value that has already been parsed as JSON

Returns:

Cookie | undefined

Remarks

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.

Example

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'))