Hello Philipp
Welcome to the Microsoft Community.
This is not a bug but a result of how the Edge JSON Viewer behaves when opening a URL programmatically via window.open. Here's an explanation of what's happening and how you can address the issue:
Explanation:
- Manual URL Entry:
- When you manually input the JSON URL in Edge's address bar, Edge knows it's serving JSON content and uses its built-in JSON Viewer to pretty-print it.
- window.open() Behavior:
- When you use window.open() to programmatically open the URL, Edge does not automatically invoke its JSON Viewer. Instead, it treats the opened page as plain text or raw JSON, and the pretty-print functionality is not applied.
- Reason:
- Edge's JSON Viewer may only be triggered when the request is a direct manual navigation or when the JSON content is properly formatted and served with the correct Content-Type (application/json) header, but window.open doesn't trigger the JSON Viewer in the same way.
Solutions:
- Manually Open the URL
- The simplest workaround is to let the user click the JSON URL (e.g.,
<a href="URL" target="_blank">Open JSON</a>), which will behave like manual navigation and trigger the JSON Viewer.
- Open the JSON in a New Tab with Pretty Formatting
- You can use a JavaScript library (e.g., JSON.stringify) to pretty-print the JSON content before displaying it in a new tab or window. Example:
This fetches the JSON data, formats it nicely, and displays it in a new window or tab using <pre> tags.fetch("https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41&hourly=temperature_2m") .then(response => response.json()) .then(data => { const prettyJSON = JSON.stringify(data, null, 2); // Pretty printconst newWindow = window.open(); newWindow.document.write(`<pre>${prettyJSON}</pre>`); newWindow.document.close(); });
- Use an External JSON Viewer Tool
- If you frequently need to pretty-print JSON, you can use tools like:
- jsonviewer.stack.hu
- jsonformatter.org
- These tools can take raw JSON data and pretty-print it in a visually appealing manner.
- Disclaimer: This is a non-Microsoft website. The page appears to be providing accurate and safe information. Watch out for ads on the site that may advertise products frequently classified as PUP (Potentially Unwanted Products). Thoroughly research any product advertised on the site before you decide to download and install it.
Summary:
- The behavior you're seeing is by design and not a bug. To ensure JSON is always pretty-printed when using window.open, either fetch the data and format it programmatically (Solution 2) or let the user manually click a link (Solution 1).
Best Regards,
William.Y | Microsoft Community Support Specialist