You could pass options as a URL parameter:

window.open( myURL + "/?options=" + JSON.stringify(options) );
Answer from Justin Ethier on Stack Overflow
🌐
W3Schools
w3schools.com › jsref › met_win_open.asp
Window open() Method
The open() method opens a new browser window, or a new tab, depending on your browser settings and the parameter values.
Top answer
1 of 4
1

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:

  1. 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.
  2. 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.
  3. 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:

  1. Manually Open the URL
  • The simplest workaround is to let the user click the JSON URL (e.g., Open JSON), which will behave like manual navigation and trigger the JSON Viewer.
  1. 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:
      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(`
    ${prettyJSON}
    `); newWindow.document.close(); });
    This fetches the JSON data, formats it nicely, and displays it in a new window or tab using
     tags.
  1. 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

2 of 4
0

Thanks, I implemented the change and and the users are now clicking on a simple link in the javascript/angular UI, if they decide to view the plain JSON data from the WebService and this is working fine. The JSONViewer is opening when clicking a simple link (was not opening when using window.open(...)).
Strange behaviour, thanks for the workaround, this did solve my problem.
Greetings
Philipp

🌐
Stack Overflow
stackoverflow.com › questions › 71585894 › how-to-pass-json-data-to-another-window-using-window-open
How to pass JSON data to another window using window.open
I wanted to pass this object in the Javascript Window.open() parameters but so far i havent found anything helpful { name:"xxx", age:21, country:"xxx" } im a beginner in this so i
🌐
Coderanch
coderanch.com › t › 493054 › languages › pass-JSON-Object-window-window
How to pass JSON Object to another window using window.open (HTML Pages with CSS and JavaScript forum at Coderanch)
April 26, 2010 - programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums · this forum made possible by our volunteer staff, including ... ... Hi I am getting JSON reponse from server into my JSP Page . Now my requirement is i ned to send this JSON response to another window using window.open.
🌐
Stack Overflow
stackoverflow.com › questions › 42480284 › how-to-open-a-new-window-and-send-out-a-post-request-with-json-data-in-the-new-w
How to open a new window and send out a POST request with JSON data in the new window using JavaScript? - Stack Overflow
The steps that I tried are: Create a new window object using window.open(). Set a JavaScript function to execute on window.onload. In the JavaScript function defined in step 2, send a XMLHttpRequest for POST data using JSON...
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 57012701 › how-to-set-a-json-file-as-a-variable-for-use-in-window-open-javascript
How To Set a .json file as a variable for use in window.open (javascript)? - Stack Overflow
July 13, 2019 - But if the web page is not from a local file, it probably won't allow it to open this. It's not specific to JSON, you'd get the same error trying to load a local HTML file. ... Why are yuo trying to load JSON into a window, anyway? JSON is not usually intended for displaying directly, it's data intended to be used by applications.
🌐
freeCodeCamp
forum.freecodecamp.org › t › window-open-vs-get › 169384
Window.open() vs $.get()
August 3, 2017 - Hi, I am running a few tests for my Quote Machine Challenge App. Besides Twitter, I’d linke to share the quotes as well to Hubzilla. Running window.open(hubzillaQueryLink) will open a new window and post fine according to the queryString. Now I thought I might want to get the JSON that’s put in the newly opened window and extract the Hubzilla Site where the quote is actually posted, so you don’t see a JSON but the actual network you are posting to.
🌐
W3Schools
w3schools.com › js › js_window.asp
JavaScript Window - The Browser Object Model
JS Window JS Screen JS Location JS History JS Navigator JS Popup Alert JS Timing JS Cookies JS Web APIs · Web APIs Intro Fetch API Geolocation API Web History API Web Pointer API Web Storage API Validation API Web Worker API JS AJAX · AJAX Intro AJAX XMLHttp AJAX Request AJAX Response AJAX XML File AJAX PHP AJAX ASP AJAX Database AJAX Applications AJAX Examples JS JSON
Top answer
1 of 16
443

This is how I solved it for my application:

HTML: <a id="downloadAnchorElem" style="display:none"></a>

JS (pure JS, not jQuery here):

var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(storageObj));
var dlAnchorElem = document.getElementById('downloadAnchorElem');
dlAnchorElem.setAttribute("href",     dataStr     );
dlAnchorElem.setAttribute("download", "scene.json");
dlAnchorElem.click();

In this case, storageObj is the js object you want to store, and "scene.json" is just an example name for the resulting file.

This approach has the following advantages over other proposed ones:

  • No HTML element needs to be clicked
  • Result will be named as you want it
  • no jQuery needed

I needed this behavior without explicit clicking since I want to trigger the download automatically at some point from js.

JS solution (no HTML required):

  function downloadObjectAsJson(exportObj, exportName){
    var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(exportObj));
    var downloadAnchorNode = document.createElement('a');
    downloadAnchorNode.setAttribute("href",     dataStr);
    downloadAnchorNode.setAttribute("download", exportName + ".json");
    document.body.appendChild(downloadAnchorNode); // required for firefox
    downloadAnchorNode.click();
    downloadAnchorNode.remove();
  }

Edit: If you'd like to format the JSON with some whitespace for better readability, you can use the additional parameters of the stringify function to do so:

  JSON.stringify(exportObj, null, 2)

See JSON.stringify on MDN, thanks TryingToImprove for the tip!

2 of 16
52

You could try using:

  • the native JavaScript API's Blob constructor and
  • the FileSaver.js saveAs() method

No need to deal with any HTML elements at all.

var data = {
    key: 'value'
};
var fileName = 'myData.json';

// Create a blob of the data
var fileToSave = new Blob([JSON.stringify(data)], {
    type: 'application/json'
});

// Save the file
saveAs(fileToSave, fileName);

If you wanted to pretty print the JSON, per this answer, you could use:

JSON.stringify(data,undefined,2)
🌐
MiniTool Partition Wizard
partitionwizard.com › home › partition magic › how to open json files on a windows computer and other devices?
How to Open JSON Files on a Windows Computer and Other Devices? - MiniTool Partition Wizard
July 12, 2023 - For example, if you want to open a JSON File using Notepad, you should right-click the JSON file, choose the Open with option, and choose Notepad. Then it will look like the following screenshot, and not only can you view it, but also edit it as well. Apart from using the above programs, you can also open JSON files through Google Chrome if the browser is installed on your Windows computer. In Google Chrome, an extension called JSONView allows you to view the JSON data in a well-structured manner.
🌐
EasyTechGuides
easytechguides.com › open-a-json-file
How to open a JSON file on Windows 10 and 11 (step by step)
JSON (JavaScript Object Notation) ... values). Open File Explorer. In File Explorer, you go to the JSON file. Right-click on the JSON file. Click on Open with....