Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript / Chrome - How to copy an object from the webkit inspector as code

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.enter image description here

like image 738
mheavers Avatar asked Apr 24 '12 20:04

mheavers


People also ask

What is object object in JavaScript?

The JavaScript [object Object] is a string representation of an object. To see the contents of an object, you should print the object to the console using console. log() or convert the object to a string. Or, you can use a for…in loop to iterate over the object and see its contents.


1 Answers

  1. Right-click an object in Chrome's console and select Store as Global Variable from the context menu. It will return something like temp1 as the variable name.

  2. Chrome also has a copy() method, so copy(temp1) in the console should copy that object to your clipboard.

Copy Javascript Object in Chrome DevTools

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.

like image 177
kevnk Avatar answered Oct 29 '22 14:10

kevnk