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.
jquery – Javascript / Chrome – How to copy an object from the webkit inspector as code
The Question :
- Try using firefox and the option .toSource(). It’s easier
The Answer 1
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 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.
The Answer 2
Try JSON.stringify()
. Copy the resulting string. Does not work with objects containing circular references.
The Answer 3
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));
The Answer 4
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
The Answer 5
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.
The Answer 6
If you’ve sent the object over a request you can copy it from the Chrome -> Network tab.
Request Payload – > View Source
The Answer 7
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; }, ' '); }
The Answer 8
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.
The Answer 9
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.
The Answer 10
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/
The Answer 11
Add this to your console and execute
copy(JSON.stringify(foo));
This copies your JSON to clipboard