free tool · no sign-up · runs in your browser

JSON to Swift Codable structs

Paste a JSON sample and get Swift structs that decode it with JSONDecoder. Properties are camelCased, and when that changes a key, like user_id to userId, a CodingKeys enum maps it back. Nested objects become their own structs, whole numbers become Int and decimals Double, and null or missing values become optionals.

paste here

result

3 types

On a Mac? Skip this tab next time. Pastely does it in any app: copy, press ⌘⇧V, and it pastes in the format that app needs.

Download Pastely free

Free to embed. Put this json to swift on your blog, docs or class page.

How JSON maps to Swift

JSONSwift
"text"String
42Int
3.5Double
trueBool
null, or missing in some itemsan optional, like String?
[…][Element]
{"plan": {…}}a nested struct Plan
"user_id"userId, with a CodingKeys case

Decoding it

let root = try JSONDecoder().decode(Root.self, from: data)
print(root.userId)

Dates arrive as String, because JSON has no date type. To decode them as Date, change the property type and set decoder.dateDecodingStrategy = .iso8601 (or a custom formatter for other formats).

JSON to Swift: questions

Why CodingKeys instead of convertFromSnakeCase?+

Both work. Setting decoder.keyDecodingStrategy = .convertFromSnakeCase converts every key, but it gets keys like user_ID or url_path wrong (it expects userId and urlPath, not userID). CodingKeys spell each mapping out, so the struct decodes the same with any decoder.

Why is a property optional?+

Because the sample had null for it, or some items in an array didn't have the key. Decoding fails if a non-optional key is missing, so optionals are the safe choice there.

How do I decode JSON with these structs?+

let root = try JSONDecoder().decode(Root.self, from: data). For an array at the top level, decode [RootItem].self, or use the Root type alias.

Is my JSON uploaded?+

No. It's converted in your browser.

More free tools

Paste as Plain Text

Strip fonts, colours and links from copied text, and the invisible characters too.

Remove Line Breaks

Fix text copied from a PDF: join broken lines back into paragraphs.

Case Converter

UPPER, lower, Title, Sentence, camelCase, snake_case and more in one click.

Word Counter

Words, characters, sentences, paragraphs and reading time as you type.

JSON Formatter

Format, validate and minify JSON, and fix trailing commas and single quotes.

Markdown Table Generator

Paste cells from Excel, Google Sheets or a CSV and get a Markdown, HTML or JSON table.

Remove Duplicate Lines

Dedupe, sort, trim and clean up any list in one paste.

Clipboard Viewer

See everything on your clipboard: plain text, HTML, images and hidden formats.

Mac Copy & Paste Shortcuts

Every copy, paste and screenshot shortcut on a Mac, searchable.

Paste Into Google Sheets

Turn any copied table, list or CSV into columns that paste straight into Sheets or Excel.

Table Converter

Convert tables between HTML, Markdown, CSV, TSV and JSON, in any direction.

JSON to CSV

Turn a JSON array into CSV or spreadsheet cells, nested objects flattened.

CSV to JSON

Turn a CSV or spreadsheet into a JSON array, with numbers and true/false typed.

ChatGPT Text Formatter

Fix the asterisks and hashes when you paste ChatGPT text into WhatsApp, Slack or a plain text box.

Markdown to Rich Text

Copy Markdown as real formatting for Google Docs, Gmail, Word and Notes.

Rich Text to Markdown

Paste from Google Docs, Word or a web page and get clean Markdown.

JSON to TypeScript

Generate TypeScript interfaces from a JSON sample, optional fields included.

Character Counter

Count characters for X, Instagram, meta tags and SMS, including how many texts a message costs.

Sort Alphabetically

Sort any list A to Z, Z to A, by length or in natural number order.