I am doing a console.log statement in my javascript in order to log a javascript object. I'm wondering if there's a way, once that's done - to copy that object as javascript code. What I'm trying to do is convert an object that was created using ajax to parse an xml feed into a static javascript object so that a file can run locally, without a server. I've included a screenshot of the object in the chrome inspector window so you can see what I'm trying to do.
-
1Try using firefox and the option .toSource(). It's easier – chepe263 Apr 24 '12 at 20:18
14 Answers
Right-click an object in Chrome's console and select
Store as Global Variable
from the context menu. It will return something liketemp1
as the variable name.Chrome also has a
copy()
method, socopy(temp1)
in the console should copy that object to your clipboard.
Note on Recursive Objects: If you're trying to copy a recursive object, you will get [object Object]
. The way out is to try copy(JSON.stringify(temp1))
, the object will be fully copied to your clipboard as a valid JSON, so you'd be able to format it as you wish, using one of many resources.
If you get the Uncaught TypeError: Converting circular structure to JSON
message, you can use JSON.stringify
's second argument (which is a filter function) to filter out the offending circular properties. See this Stack Overflow answer for more details.

- 18,733
- 3
- 28
- 30
-
3return undefined in chrome Version 49.0.2623.87 (64-bit) ? why >? – Pardeep Jain Aug 19 '16 at 06:33
-
13@PardeepJain - that is expected from the copy() method because there is nothing to return. The data should be in your clipboard. – Carl Aug 23 '16 at 17:57
-
70
-
1@Ullallulloo try logging out with JSON.stringify like this: https://stackoverflow.com/a/4293047/622287 – kevnk May 24 '17 at 21:06
-
Works! Unfortunately, it seems to give the most recent version of that object at script endtime, not at the time of console.info. – HoldOffHunger Jul 29 '17 at 00:18
-
@HoldOffHunger in that case, I'd try logging it out as `JSON.stringify(yourObject)` instead – kevnk Aug 01 '17 at 20:49
-
@kevnk: Yeap, that worked, thanks! Exactly what I did --> function logObject(object) { console.info(JSON.stringify(object).replace(/,/g, ",\n")); } – HoldOffHunger Aug 01 '17 at 21:09
-
-
@ToniLeigh What browser version are you using? This is still working for me on Chrome 62.0.3202.94 – kevnk Dec 07 '17 at 21:37
-
Doesn't work using version 64.0.3282.140, any other way to get your hands on this without using the `Store as global variable` option? – Ryan Wilson Feb 12 '18 at 22:26
-
6it works only if you have a shallow JS object, if you have recursive deep object then will you ll get [Object Object] - that is expected – Marwen Trabelsi Feb 23 '18 at 14:02
-
1
-
1
-
1
-
@SuperUberDuper - I'm on v75 and the "Store as Global Variable" option exists for objects and arrays... – kevnk Jul 03 '19 at 20:00
-
@kevnk it works for recursive when used as console.log(JSON.stringify(tepm1)) – cnsnaveen Jul 18 '19 at 12:50
-
Thanks! But how to remove it from global variables? Now every time it pops up in the console. – Tarik Hacialiogullari Aug 21 '19 at 05:58
-
Worked fine with three depth levels: `{50: {'20-10-19': {amount: 5, info: {'id': 'test'}}}}` – Giorgio Tempesta Oct 15 '19 at 07:08
-
1Unfortunutely when I right click the object on google chrome, `Store as global variable` option does not show up. – Onat Korucu Nov 04 '19 at 07:12
-
-
The recursive issue was the cause for me to get `[object Object]`! Saved my day. That's definitely not an useful message to understand what's going on. – Shawn Dec 03 '20 at 00:32
-
1`JSON.stringify()` can't handle circular references: `Uncaught TypeError: Converting circular structure to JSON` – Jacob Jun 16 '21 at 21:01
-
@Jacob - Thanks! I've updated the answer. See this other overflow answer for more details: https://stackoverflow.com/a/12659424/622287 – kevnk Jun 17 '21 at 16:40
-
1Does not work with the window object, I get "TypeError: Converting circular structure to JSON" – Black Oct 26 '21 at 11:47
In Chrome 89 or later you can simply right click an object in the console and choose Copy Object
(ref). This also works in some other places inside Chrome Developer Tools e.g. whilst debugging or inside response tab for a network request.
Other option is to use the copy
command as-is:
var x = { a: 1, b: 2 };
copy(x);

- 262,204
- 82
- 430
- 521
-
7I don't see how that would work unless you modify the code that logs it. – iConnor Nov 10 '13 at 21:27
-
37
You can copy an object to your clip board using copy(JSON.stringify(Object_Name)); in the console.
Eg:- Copy & Paste the below code in your console and press ENTER. Now, try to paste(CTRL+V for Windows or CMD+V for mac) it some where else and you will get {"name":"Daniel","age":25}
var profile = {
name: "Daniel",
age: 25
};
copy(JSON.stringify(profile));

- 3,634
- 2
- 27
- 27
-
21Doesn't work with DOM nodes, window or any other object that it's circular – Neithan Max Dec 27 '16 at 03:18
-
-
You can now accomplish this in Chrome by right clicking on the object and selecting "Store as Global Variable": http://www.youtube.com/watch?v=qALFiTlVWdg

- 8,315
- 4
- 30
- 23
-
2As of Version 39.0.2171.95, the "Store as Global Variable" option is not available when inspecting Android devices with Chrome. – Walter Roman Jan 13 '15 at 00:20
-
1@David Calhoun, I voted for your answer. It looks like your answer was posted June 12, 2014 and the one accepted was Aug 5, 2014, largely taking exactly what you had. I have to admit that he mentions temp1 where your answer only shows it in your video, so maybe that's why the other answer was accepted. Best wishes. – PatS Sep 15 '18 at 16:01
If you've sent the object over a request you can copy it from the Chrome -> Network tab.
Request Payload - > View Source

- 10,678
- 10
- 55
- 94
-
2After copying parsed payload, you can format json from https://jsonformatter.curiousconcept.com/. – Muhammad Hassan Oct 20 '16 at 08:56
Follow the following steps:
- Output the object with console.log from your code, like so: console.log(myObject)
- Right click on the object and click "Store as Global Object". Chrome would print the name of the variable at this point. Let's assume it's called "temp1".
- In the console, type:
JSON.stringify(temp1)
. - At this point you will see the entire JSON object as a string that you can copy/paste.
- You can use online tools like http://www.jsoneditoronline.org/ to prettify your string at this point.

- 18,769
- 10
- 104
- 133

- 3,623
- 1
- 25
- 24
-
Step with JSON.stringify(temp1) could affect to a long time execution if object is big. – heroin Jul 10 '16 at 13:41
-
@JoeTidee I had the same issue, but I set up a `debugger` statement and then retrieved my var directly from the console at the breakpoint. – Tony Brasunas Aug 02 '18 at 17:28
Update - Chrome 89
Right click -> Copy object
source: (https://developers.google.com/web/updates/2021/01/devtools?utm_source=devtools)
also from debugger

- 3,232
- 1
- 19
- 19
-
-
amm It's really not a solve one issue, but you can do manipulation to have the key and value if you have `Object.entries({test: undefined, test2: 2}).map(([key,value])=>({key,value}))` and after copy you will get `[ { "key": "test" }, { "key": "test2", "value": 2 } ]` – Omer Jul 14 '21 at 10:58
-
1People, can you just STOP posting those invalid answers already? Using the `copy()` function on an object only EVER results in the exact string `[object Object]` being copied. It's useless! – Szczepan Hołyszewski Aug 09 '22 at 09:30
This actually helped me out mine is a bit of an edge case. But for what I am doing it works.
The devices I am testing on use safari debug tools and I can never copy the objects like you can in Chrome simply right click and copy object.
Tried JSON.stringify and the pasting the contents into https://beautifier.io but then have to try reformat it.
I ended up using local storage and the copy method.
In your code use.
localStorage.setItem('dataCopy', JSON.stringify(data));
Then just paste this in the console and click enter.
copy(JSON.parse(window.localStorage.dataCopy))
You then have your array of objects in the clip board.

- 3,872
- 13
- 44
- 78
This should help stringify deep objects by leaving out recursive Window
and Node
objects.
function stringifyObject(e) {
const obj = {};
for (let k in e) {
obj[k] = e[k];
}
return JSON.stringify(obj, (k, v) => {
if (v instanceof Node) return 'Node';
if (v instanceof Window) return 'Window';
return v;
}, ' ');
}

- 315
- 1
- 13
-
Does not work: Uncaught TypeError: Converting circular structure to JSON --> starting at object with constructor 'Window' – Black Oct 19 '22 at 13:31
you can console the object as string
var objToString = JSON.stringify(obj)
console.log(objToString );
Then in an editor like Notepad ++ paste de output and then ehit a plugin format
Using "Store as a Global Variable" works, but it only gets the final instance of the object, and not the moment the object is being logged (since you're likely wanting to compare changes to the object as they happen). To get the object at its exact point in time of being modified, I use this...
function logObject(object) {
console.info(JSON.stringify(object).replace(/,/g, ",\n"));
}
Call it like so...
logObject(puzzle);
You may want to remove the .replace(/./g, ",\n") regex if your data happens to have comma's in it.

- 18,769
- 10
- 104
- 133
So,. I had this issue,. except I got [object object]
I'm sure you could do this with recursion but this worked for me:
Here is what I did in my console:
var object_that_is_not_shallow = $("all_obects_with_this_class_name");
var str = '';
object_that_is_not_shallow.map(function(_,e){
str += $(e).html();
});
copy(str);
Then paste into your editor.

- 405
- 3
- 6
Right click on data which you want to store
- Firstly, Right click on data which you want to store -> select "Store as global variable" And the new temp variable appear like bellow: (temp3 variable): New temp variable appear in console
- Second use command copy(temp_variable_name) like picture: enter image description here After that, you can paste data to anywhere you want. hope useful/

- 107
- 1
- 4
Add this to your console and execute
copy(JSON.stringify(foo));
This copies your JSON to clipboard

- 3,752
- 35
- 31
- 35

- 1