All Articles

How to copy an object from the Chrome inspector console as code

Have you ever do console.log an object and wondering how can copy the object over? You have a few options.

Solution 1. Copy, and you are all set!

  1. Right-click to the object, select Store as global variable. Usually, Chrome stored it as temp1
  2. Type copy(temp1)
  3. You can paste now 😆

How to copy an object from the Chrome inspector console as code

Note: If you’re trying to copy a recursive object, you will get [object Object] when pasting. See the solution 2.

Solution 2: Programmatically do JSON.stringify and copy the string 🤪

  1. Right-click to the object, select Store as global variable. Usually, Chrome stored it as temp1
  2. Type copy(JSON.stringify(temp1))
  3. There you have it!

How to copy an object from the Chrome inspector console as code

Bonus: How to copy JSON as TypeScrip type

  1. Install VSCode Paste JSON as Code extension
  2. Follow either solution 1 and 2 to copy the object
  3. Open VSCode, select View -> Command Pallette or press Command + Shift + P
  4. Select Paste JSON as Types
  5. You have the type now. It supports many languages such as C# or Java, not just TypeScript.

How to copy an object from the Chrome inspector console as code

Published 10 Oct 2020

Recent Posts

The different between type and interface in TypeScript

Should you use type or interface? For complex types, you have no choice: you need to use a type alias. But what about the simpler object types that can be represented either way? To answer this question, you should consider consistency and augmentation. Are you working in a codebase that consistently uses interface? Then stick with interface. Does it use type? Then use type.

Use async functions instead of callbacks for asynchronous code

Prefer async/await to raw Promise when possible. They produce more concise, straightforward code and eliminate whole classes of errors.


Follow @tuantrungvo on Twitter for more!