Prompt
How do I normalize scraped page data into JSON?
Latest observation
A good way to normalize scraped page data into JSON is to separate it into three stages:
- Extract raw values from the page
- Clean/standardize those values
- Map them into a consistent JSON schema
1) Define the JSON shape first
Before scraping, decide what the final JSON should look like.
Example schema:
{
"title": "",
"price": 0,
"currency": "USD",
"url": "",
"availability": "",
"images": [],
"breadcrumbs": [],
"specs": {},
"scraped_at": ""
}
Having a fixed schema makes normalization much easier.
2) Normalize common data types
Web pages often contain messy data. Convert them into predictable types.
Strings
- Trim whitespace
- Collapse repeated spaces
- Remove invisible characters
function normalizeText(value) {
return String(value || "")
.replace(/\s+/g, " ")
.trim();
}
Numbers
Convert currency/price strings to numeric values.
function parsePrice(value) {
const cleaned = String(value || "")
.replace(/[^0-9.,-]/g, "")
.replace(/,/g, "");
return cleaned ? Number(cleaned) : null;
}
URLs
Resolve relative links to absolute URLs.
function normalizeUrl(url, baseUrl) {
try {
return new URL(url, baseUrl).toString();
} catch {
return null;
}
}
Dates
Convert dates into ISO format.
function normalizeDate(value) {
const d = new Date(value);
return isNaN(d) ? null : d.toISOString();
}
Booleans
Map textual values to true/false.
function parseBoolean(value) {
const s = String(value || "").toLowerCase();
return ["true", "yes", "in stock", "available"].includes(s);
}
3) Clean arrays and objects
Arrays
Remove empty items and normalize each entry.
function normalizeArray(arr, itemFn) {
return (arr || [])
.map(itemFn)
.filter(v => v !== null && v !== undefined && v !== "");
}
Key-value specs
Turn tables or definition lists into an object.
function normalizeSpecs(pairs) {
const out = {};
for (const { key, value } of pairs) {
const k = normalizeText(key).toLowerCase();
const v = normalizeText(value);
if (k) out[k] = v;
}
return out;
}
4) Build a transformation layer
Create one function that takes raw scraped data and returns normalized JSON.
function normalizeProduct(raw, baseUrl) {
return {
title: normalizeText(raw.title),
price: parsePrice(raw.price),
currency: normalizeText(raw.currency) || null,
url: normalizeUrl(raw.url, baseUrl),
availability: normalizeText(raw.availability).toLowerCase(),
images: normalizeArray(raw.images, img => normalizeUrl(img, baseUrl)),
breadcrumbs: normalizeArray(raw.breadcrumbs, normalizeText),
specs: normalizeSpecs(raw.specs || []),
scraped_at: new Date().toISOString()
};
}
Example input:
const raw = {
title: " Fancy Shoes ",
price: "$129.99",
currency: "USD",
url: "/product/123",
availability: "In Stock",
images: ["/img1.jpg", " /img2.jpg "],
breadcrumbs: ["Home", "Shoes", "Sneakers"],
specs: [
{ key: "Color", value: "Black" },
{ key: "Size", value: "42" }
]
};
Output:
{
"title": "Fancy Shoes",
"price": 129.99,
"currency": "USD",
"url": "https://example.com/product/123",
"availability": "in stock",
"images": [
"https://example.com/img1.jpg",
"https://example.com/img2.jpg"
],
"breadcrumbs": ["Home", "Shoes", "Sneakers"],
"specs": {
"color": "Black",
"size": "42"
},
"scraped_at": "2026-11-20T..."
}
5) Handle missing and inconsistent data
Scraped pages vary a lot, so use rules like:
- Missing strings →
nullor"" - Missing numbers →
null - Missing arrays →
[] - Unknown values → keep raw value in a fallback field if needed
Example:
{
"price": null,
"price_raw": "Call for price"
}
This is especially useful when some pages don’t fit the expected pattern.
6) Validate the final JSON
After normalization, validate against a schema so bad data doesn’t slip through.
Example with JSON Schema:
- required fields
- correct types
- allowed enums
You can use libraries like:
- Ajv for JavaScript
- Pydantic for Python
- Zod for TypeScript
7) Best practices
- Keep raw scraped data separate from normalized output
- Use a single canonical schema
- Normalize at the edge, before storing or sending data
- Preserve raw values for debugging
- Add logging for unexpected formats
- Make normalization functions reusable and testable
Simple workflow
- Scrape raw page data
- Extract fields into a raw object
- Normalize each field
- Validate schema
- Store as JSON
If you want, I can also show:
- a Python example
- a JavaScript/Node example
- a JSON Schema for validation
- or a real scraper pipeline using BeautifulSoup / Cheerio / Playwright