Chrome: How to Copy an Object or Array from the Console tab

avatar
Borislav Hadzhiev

Last updated: Mar 7, 2024
6 min

banner

# Table of Contents

  1. Chrome: How to Copy an Object or Array from the Console tab
  2. Copy an Object or Array in Chrome Console tab by storing it as a global variable
  3. Copy an object or array in Chrome Console by defining the variable directly
  4. Copy an object or an array from the Network tab in Chrome
  5. Copy an Object or Array in Chrome from the Sources tab when debugging

# Chrome: How to Copy an Object or Array from the Console tab

The easiest way to copy an object or an array from the Console tab is to:

  1. console.log() the object or array.
  2. Right-click on the object or array in the Console tab.
  3. Select Copy object.
  4. The object or array is copied to your clipboard, you can press Ctrl + V to paste it.

copy object using copy object menu

I passed the following object to the console.log() method in the example.

index.js
const obj = { id: 1, first: 'bobby', last: 'hadz', salary: 500, address: { country: 'Belgium', city: 'Ghent', }, }; console.log(obj);

Your object or array could be as deeply nested as necessary and this approach works.

Make sure you have selected the Console tab in your browser's developer tools:

  1. Right-click on the page and click Inspect or simply press F12.

right click inspect

  1. Click on the Console tab and console.log() the object.

select console tab and copy object or array

# Copy an Object or Array in Chrome Console tab by storing it as a global variable

Alternatively, you can copy an object or an array in the Chrome Console tab by storing it as a global variable:

  1. console.log the object or the array.
  2. In your Console tab, right-click on the object or array and select Store as Global Variable.
  3. The global variable will have a name such as temp1.
  4. Use the copy() function to copy the object or array to your clipboard, e.g. copy(temp1).
Console
copy(temp1)

Here is a short clip that demonstrates how this works.

copy object or array by storing it as global variable

You can then use Ctrl + V (or Cmd + V on macOS) to paste the copied object or array.

Note that the copy() method copies the object or array to your clipboard and returns undefined.

If you work with a recursive object or array (one that references itself), you might get [object Object] when you pass it to the copy() function.

To resolve the issue, try to pass the object to the JSON.stringify method before passing it to the copy() function.

Console
copy(JSON.stringify(temp1))

using copy function with json stringify

If you get the error TypeError: Converting circular structure to JSON, click on the link and follow the instructions.

# Copy an object or array in Chrome Console by defining the variable directly

Another way to copy an object or an array from the Console tab in Chrome is to define the variable directly in the Console tab and use the copy() function.

For example, you could paste the following to your Console tab to copy the obj variable to your clipboard.

Console
const obj = { id: 1, first: 'bobby', last: 'hadz', salary: 500, address: { country: 'Belgium', city: 'Ghent', }, }; copy(obj)

define variable directly in console tab

If you need to copy a recursive object or an array, use the JSON.stringify() method when calling the copy() function.

Console
const obj = { id: 1, first: 'bobby', last: 'hadz', salary: 500, address: { country: 'Belgium', city: 'Ghent', }, }; copy(JSON.stringify(obj))

The JSON.stringify() method converts the given object or array to a JSON string.

If you get the error TypeError: Converting circular structure to JSON, click on the link and follow the instructions.

# Copy an object or an array from the Network tab in Chrome

If you are sending the object or array in an HTTP request (e.g. POST or PUT), you can copy it from the Network tab.

If you need to copy an object or an array from an HTTP response, you could also copy it from the Network tab.

  1. Click on the Network tab in your developer tools.

click network tab

If you don't see the Network tab, click on the >> icon as shown in the screenshot and select Network.

  1. Select the request from the list in the left sidebar.
  2. Click on Payload or Preview on the right.
  3. Right-click on the object or array and click on Copy value or Copy object to copy the object or array.

copy object or array from network tab

  1. Press Ctrl + V (or Cmd + V) to paste the copied object or array.

Here is a short clip that demonstrates how this works.

Make sure the Network tab is selected to be able to view the HTTP requests.

copy object or array from network tab

You can also copy the object or array by using the Store object as a global variable option.

  1. Click on the Network tab in your developer tools.

click network tab

If you don't see the Network tab, click on the >> icon as shown in the screenshot and select Network.

  1. Select the request from the list in the left sidebar.
  2. Click on Payload or Preview on the right.
  3. Right-click on the object or array and select Store object as global variable.

store object as global variable from network tab

  1. The Console tab will open and the global variable will have a name similar to temp1.
  2. Use the copy() function to copy the object or array to your clipboard.
Console
copy(temp1)
  1. If you work with recursive objects or arrays, use the JSON.stringify() method.
Console
copy(JSON.stringify(temp1))

If you get the error TypeError: Converting circular structure to JSON, click on the link and follow the instructions.

# Copy an Object or Array in Chrome from the Sources tab when debugging

You can also copy an object or an array from the Sources tab in Chrome when debugging.

  1. Select the Sources tab in your Chrome developer tools.

copy object or array when debugging chrome

If you don't see the Sources tab, click on the >> icon and select Sources from the dropdown menu.

  1. Click on the file in which the object is located in the left sidebar (e.g. index.js).

  2. Add a breakpoint right after the object or array you want to copy.

Make sure the breakpoint is added after the object or array, otherwise, it would be undefined.

You can add a breakpoint by left-clicking on a line number in the code editor to the right.

  1. Refresh the page by pressing F5.
  2. Your code should be paused on the line with the breakpoint.
  3. Below the code editor, you should be able to see the Scope tab.
  4. Right-click on the object in the Scope tab and select Copy object.

right click in scope tab and select copy object

  1. The object or array is copied to your clipboard. You can use Ctrl + V to paste it.

Alternatively, you can right-click on the object in the Scope tab and select Store object as global variable.

right click in scope and select store object as global variable

Your Console tab will open and the global variable will have a name similar to temp1.

Use the copy() function to copy the object or array to your clipboard.

Console
copy(temp1)

If you work with recursive objects or arrays, use the JSON.stringify() method.

Console
copy(JSON.stringify(temp1))

If you get the error TypeError: Converting circular structure to JSON, click on the link and follow the instructions.

I've also written an article on how to edit and replay XHR (HTTP) requests in Chrome and Firefox.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.