228

Using Chrome Developer Tools, i have printed a JSON object with console.log.

is there a way that I can copy it to the clipboard?

Francisco Tapia
  • 2,614
  • 4
  • 24
  • 43
Anagio
  • 3,088
  • 5
  • 21
  • 29

4 Answers4

403

If the right-click -> copy is not available you could try:

1 - Right-click the object and select "Store as global variable"

2 - The console will print the new variable's name, for example:

//temp1

3 - Type:

copy(temp1)   

The object is now available in your clipboard.

Tested in chrome 36

Bertrand
  • 4,146
  • 1
  • 13
  • 5
  • 3
    In Chrome 41 this returns `undefined`. – isherwood Mar 20 '15 at 14:32
  • @isherwood I've jsut tested in Chrome 41 and still works, you might be missing something. – Bertrand Mar 20 '15 at 17:56
  • what do you mean by " Right-click the object" ? – RollRoll Apr 01 '15 at 21:47
  • 1
    @EdwinSnts Bertrand means in the Javascript console, after logging an object with `console.log( myObject )`, to right click on that object. It will give you a context menu with the "Store as global variable" feature. – BradGreens May 08 '15 at 16:37
  • 14
    `copy(temp1)` works in Chrome 46, but copying a larger object (a jQuery.Event object), the clipboard gets `[object Object]`. – sealocal Nov 09 '15 at 19:33
  • 8
    In Chrome `copy(temp1)` returns `undefined` but the object is available in your clipboard. – Suzana Jan 27 '16 at 16:42
  • 1
    Does this work programatically too, or only through the console? (e.g forcing content on the user's clipboard) – Hanna Feb 16 '16 at 18:48
  • 1
    Also, make sure is selected or Store as Global Variable won't work. – JDavis Feb 18 '16 at 20:35
  • @Johannes seems to work programaticaly for me – rex Mar 27 '17 at 10:41
  • This is the best way to capture the JSON from console. Is there a Chrome extension that does this and downloads to a log file? – smokinguns Aug 29 '18 at 23:41
  • 3
    @sealocal: Yes, I also experienced this with a large object. Kind of irritating, because it is speacially on large objects this would be useful. – awe Sep 28 '18 at 06:59
  • 1
    So simple, I had no idea. Love it! – Jordan Aug 27 '19 at 19:44
  • Link to docs: https://developer.chrome.com/docs/devtools/console/utilities/#copy – Boris Verkhovskiy Apr 22 '21 at 21:55
  • 1
    `copy(myVariable)` even has it pretty-printed. Awesome! – Sophia Feng Apr 27 '21 at 16:33
  • 1
    @sealocal I was getting vexed about this too, but it turns out it does that [Object object] thing if the object has any recursive parts. In the console do a={b:4} then a.c=a and then copy(a) and try to paste. I ended up deleting portions of the object. – mgraham Apr 06 '23 at 15:00
4

Another simple method...from the console surround the json with JSON.stringify(yourobjecthere). Then highlight the text or optionally select the Copy button in the developer bar if it exceeds X number of rows. Hope this helps someone.

Example:

JSON.stringify(JSON.parse(window.atob(localStorage.getItem('C_C_M'))))
Rob
  • 141
  • 2
2

The answer given by @Bertrand works.But it won't when there is already an element by name copy in the Dom.

Doing copy(temp1) gave me Uncaught TypeError: copy is not a function

So I removed that element from the Dom in my console using the below line: document.querySelector('#copy').remove()

Now copy(temp1) works!

adi
  • 121
  • 1
0

Select the text in the console, then use right click -> copy

To copy the entire log (when I needed): hit ctrl-a (select all) then ctrl-c (copy)

Note: Since posting this I noticed sometimes it is necessary to select a little text before these steps work. Also for a long console output scroll to the top of the console and select a little text first. Grrr... still this is easier than saving as a file.

== Above is using Chrome 35 ==

Paul Lockwood
  • 298
  • 1
  • 2
  • 11