Plenty of sites keep their page inventory in JSON: it falls out of a build script, a CMS export or a spreadsheet someone converted months ago. Sitemaps, though, are an XML format with a fixed structure, and search engines expect that structure. Bridging the two is not a retyping job. It is a shaping job.

The JSON to XML Converter on Ovatly turns JSON into XML by fixed mapping rules, and a sitemap happens to fit those rules well once the feed is arranged for them. This guide shows how to arrange it, what the converted file looks like, and what you still need to check yourself, because the converter will not check it for you.

Contents

  • Quick answer
  • The sitemap shape you are aiming at
  • The mapping rules that build it
  • Shaping your JSON feed
  • How to convert, step by step
  • Worked example: a two-URL sitemap
  • Limitations and honest tradeoffs
  • Common mistakes
  • FAQs
  • Conclusion
  • References

Quick answer

To convert a JSON feed into an XML sitemap format: build one object named urlset, put the sitemap namespace in it as an @xmlns attribute key, and hold every page in a single url array where each item is an object with a loc key. Convert with the JSON to XML Converter, choosing urlset as the root element name, and download the result. Then check it yourself: the converter adds no XML declaration and does not validate against the sitemap schema, so the file needs your eyes before it goes anywhere.

The sitemap shape you are aiming at

A sitemap file has one root element, <urlset>, carrying the sitemap namespace as an attribute. Inside it sits one <url> element per page, and inside each <url> sits a <loc> element holding the page address. Optional fields such as last modified dates follow the same pattern, one element per value inside the <url> for that page.

Three features of that shape matter for conversion. The namespace is an attribute, not an element. The pages repeat as identical elements, not as individually named entries. And every value is simple text inside an element. Each of those features maps cleanly onto one of the converter's rules, which is why this conversion works at all.

The mapping rules that build it

Keys become elements, so a key named loc becomes a <loc> element. A key starting with @ becomes an attribute on its parent element, which is how the namespace reaches the root. An array repeats its element once per item, which is how one url key produces many <url> elements. You choose the root element name when you convert.

Special characters in values are escaped, which matters for URLs: a query string containing an ampersand is written in escaped form, as valid XML requires. Keys that are not valid XML names are renamed with underscores, a behaviour to watch for if your feed uses unusual field names.

Shaping your JSON feed

Start from whatever list of pages you have and rebuild it into one object with three levels. The top level holds a single key, urlset. Inside it, place the @xmlns key with the sitemap namespace as its value, and a url key holding an array. Each item in the array is one page: an object with a loc key and the page address as its value.

If your feed carries extra fields per page, such as a last modified date, add them as further keys inside each page object, named exactly as the sitemap format names its elements. Keep every page in the same array. Pages stored as separate keys, one per page, would convert once each under different names, and the result would not be a sitemap at all.

How to convert, step by step

  1. Shape your feed into the urlset structure described above, with the namespace as @xmlns and all pages in one url array.
  2. Open the JSON to XML Converter and paste the shaped JSON.
  3. Set the root element name to urlset and convert.
  4. Download the result as converted.xml and open it to check the structure: namespace on the root, one <url> per page, addresses correct.
  5. Rename the file to sitemap.xml, add the XML declaration line at the top if your submission route requires it, and submit.

Worked example: a two-URL sitemap

This shaped feed holds two pages:

{
  "urlset": {
    "@xmlns": "http://www.sitemaps.org/schemas/sitemap/0.9",
    "url": [
      { "loc": "https://example.com/" },
      { "loc": "https://example.com/about" }
    ]
  }
}

Converted with urlset as the root, the output is:

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>https://example.com/</loc>
  </url>
  <url>
    <loc>https://example.com/about</loc>
  </url>
</urlset>

Read it against the mapping rules. The @xmlns key became the xmlns attribute on the root element. The url array repeated its element, once per item, and each item's loc key became the <loc> element inside. A feed with two hundred pages shaped the same way produces two hundred <url> elements from the same single key.

Limitations and honest tradeoffs

Two gaps matter, and both are yours to close. The converter adds no XML declaration, so the output begins directly with the root element. Many tools accept a sitemap without the declaration, but if yours does not, add the standard declaration line yourself at the top of the file. And the converter does not validate against the sitemap schema. It produces one consistent dialect from its mapping rules; whether that output satisfies every sitemap requirement is a check you run, not a guarantee it gives.

Shaping is also manual work. The converter will not gather scattered page entries into a url array for you, so a messy feed needs arranging before conversion, not after.

Common mistakes

  • Writing xmlns as a plain key. Without the @ prefix it becomes a child element inside <urlset> instead of the namespace attribute on it.
  • Storing each page under its own key. Only arrays repeat, so separate keys produce separate element names and a file no sitemap reader accepts.
  • Submitting the output unchecked. The converter does not validate against the sitemap schema, and it adds no XML declaration.
  • Naming extra fields freely. A date field converts under whatever key name you give it, so use the element names the sitemap format defines.
  • Converting before shaping. An unshaped feed converts into valid XML that is not a sitemap. Shape first, convert second.

FAQs

How does the sitemap namespace get into the XML? Carry it in the JSON as an attribute key named @xmlns inside the urlset object. Keys starting with @ become attributes, so the namespace lands on the urlset element exactly where the sitemap format expects it.

Why does each page need to be an item in an array? Arrays are what repeat in this mapping. A url array with one object per page produces one url element per page. Pages stored under separate keys would each convert once under different element names instead.

Does the converter add the XML declaration at the top? No. The output starts with the root element you chose. If the place you submit the sitemap requires the declaration line, add it yourself at the top of the file.

Will the converter check my sitemap against the sitemap rules? No. It applies its mapping rules and produces one consistent dialect. It does not validate against the sitemap schema, so check the structure, namespace and URLs yourself before submitting.

What happens to a URL containing an ampersand? Special characters in values are escaped during conversion, so an ampersand in a query string is written in its escaped XML form, which is what a valid XML file requires.

Conclusion

A sitemap is one of the friendlier XML formats to reach from JSON, because its repeating pages and single namespace attribute line up with the converter's rules. Shape the feed into a urlset object with an @xmlns key and one url array, convert, then do the two checks the tool leaves to you: the missing declaration and the unvalidated schema. If your conversions keep going wrong in the same places instead, our guide to JSON to XML mistakes with attributes, arrays and lost structure names each failure and its fix. For more guides in this series, see our Data and JSON Tools guides.

References