# Convert cookie to object

Parse a Cookie header string into name–value pairs.

Canonical: https://1loc.dev/misc/convert-cookie-to-object/
Version: 1.0.0
Runtime: JavaScript · Node.js 22 / modern browsers
Verification: Local fixtures; no external services
Updated: 2026-10-28

## Implementation

```javascript
function parseCookies(cookie) { return Object.fromEntries(cookie.split(';').filter(item => item.includes('=')).map(item => [item.slice(0, item.indexOf('=')).trim(), item.slice(item.indexOf('=') + 1).trim()])); }
```

## Input

```json
"theme=dark; session=abc=def; lang=pt-BR"
```

## Expected output

```json
{
  "theme": "dark",
  "session": "abc=def",
  "lang": "pt-BR"
}
```

## Edge case

Input:
```json
""
```
Output:
```json
{}
```

## Limits

- The original read document.cookie and split on every =, which cut values such as "abc=def"; this version parses any header string and splits on the first = only.
- Values are not URI-decoded; apply decodeURIComponent where your app encodes them.

The lab validates types and size before calling this function. Add equivalent validation at your own trust boundary.

## Source and license

Original: https://github.com/phuocng/1loc/blob/14b481cf60dff1620ab1bb99a43b1f68068cdd9b/contents/convert-cookie-to-object.mdx
Author: Nguyen Huu Phuoc and original contributors
Contributors: See original repository history
Source commit: 14b481cf60dff1620ab1bb99a43b1f68068cdd9b
Copyright (c) 2020 phuoc-ng
License: MIT — https://1loc.dev/licenses/
Maintainer: Oveyon
Modifications: Parses any Cookie header string instead of document.cookie and splits on the first = only; the original cut values containing =. Original MDX retained as inert source evidence.
