Moment For Technology

Save the JavaScript variables in the Chrome debugger to a local JSON file

Posted on Aug. 10, 2023, 10:43 a.m. by Lauren Mendoza
Category: javascript Tag: javascript chrome json google google-maps

I've written a series of articles to collect some of the tools and tricks that programmers can use to improve their productivity.

In the Chrome Developer Tools Console TAB, you can type JavaScript variables and press Enter to see the values of those variables.

For example, I use the jQuery-like selector syntax var button = $('button') to return all instances of button tags on the current page.

If I want to save this variable locally for later analysis, one option is to use JSSON.stringfy to make its sequence number a JSON string, then manually select that string, create a new local file, copy it, and save it.

It works but it's too much trouble. Now I'm going to share with you an automated approach.

(function(console){ = function(data, filename){

if(! data) {

console.error(' No data')



if(! filename) filename = 'console.json'

if(typeof data === "object"){

data = JSON.stringify(data, undefined, 4)


var blob = new Blob([data], {type: 'text/json'}),

e = document.createEvent('MouseEvents'),

a = document.createElement('a') = filename

a.href = window.URL.createObjectURL(blob)

a.dataset.downloadurl = ['text/json',, a.href].join(':')

e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)




Paste this code into the Console TAB of the Chrome Developer Tools and execute it, adding a save method to the standard Console object. This method takes two input parameters, the first being the JavaScript variable of the JSON file at the location to be saved, and the second being the name of the local JSON file.

Returning to the example above, I executed the above JavaScript code on the Chrome Developer Tools Console page, followed by the following statement:, "button.json");

Enter and Chrome automatically pops up a JSON file save window:

Save it locally. This approach saves the programmer the work of serializing JavaScript variables into JSON strings and saving the local files manually, which improves the programmer's work efficiency.

For more of Jerry's original technical articles, please follow the official account "Wang Zixi" or scan the QR code below:

About (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.