Reputation: 30158
I am doing a console.log
statement in my javascript in order to log a javascript object. Is there 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.
Upvotes: 656
Views: 271280
Reputation: 71
This function will work for array objects if you face error:
Uncaught TypeError: Converting circular structure to JSON --> starting at object with constructor 'Object' | property 'fields' -> error:
function copyWithCircularRefs(input) {
const getCircularReplacer = () => {
const seen = new WeakSet();
return (key, value) => {
if (typeof value === "object" && value !== null) {
if (seen.has(value)) {
// Instead of returning undefined, return a custom placeholder
return '[Circular]';
}
seen.add(value);
}
return value;
};
};
// Use the custom replacer function with JSON.stringify
const stringifiedData = JSON.stringify(input, getCircularReplacer(), 2);
// Copy the stringified data to the clipboard
copy(stringifiedData);
// Log a message to the console to confirm the action
console.log('Object copied to clipboard');
}
// Usage:
// Replace 'myComplexObject' with your actual complex object
copyWithCircularRefs(myComplexObject);
This function will now handle circular references by replacing them with the string '[Circular]'. When you paste the copied data, you'll see this placeholder wherever a circular reference was detected, which allows you to understand the structure without losing the reference information.
Upvotes: 3
Reputation: 1
If you have bigint
data in JSON, all the answers do not work.
When you try to serialize an object that contains bigint
values using JSON.stringify()
, it throws a TypeError because it doesn't know how to represent bigint
values as strings.
You need a usage code:
copy(JSON.stringify(temp1, (_, v) => typeof v === 'bigint' ? v.toString() : v))
This function (_, v) => typeof v === 'bigint' ? v.toString() : v)
checks if the value is of type bigint
. If so, it converts the value to a string using toString()
. Otherwise, it leaves the value unchanged. This way, you can serialize objects containing bigint
values without encountering errors.
Screenshot copy bigint from chrome console
Upvotes: 0
Reputation: 19099
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.
Chrome also has a copy()
method, so copy(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 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.
Upvotes: 1628
Reputation: 4290
This actually helped me out mine is a bit of an edge case. But for what I am doing it works.
The devices I am testing on use safari debug tools and I can never copy the objects like you can in Chrome simply right click and copy object.
Tried JSON.stringify and the pasting the contents into https://beautifier.io but then have to try reformat it.
I ended up using local storage and the copy method.
In your code use.
localStorage.setItem('dataCopy', JSON.stringify(data));
Then just paste this in the console and click enter.
copy(JSON.parse(window.localStorage.dataCopy))
You then have your array of objects in the clip board.
Upvotes: 5
Reputation: 272006
In Chrome 89 or later you can simply right click an object in the console and choose Copy Object
(ref). This also works in some other places inside Chrome Developer Tools e.g. whilst debugging or inside response tab for a network request.
Other option is to use the copy
command as-is:
var x = { a: 1, b: 2 };
copy(x);
Upvotes: 75
Reputation: 19
you can console the object as string
var objToString = JSON.stringify(obj)
console.log(objToString );
Then in an editor like Notepad ++ paste de output and then ehit a plugin format
Upvotes: 1
Reputation: 3466
Right click -> Copy object
source: (https://developers.google.com/web/updates/2021/01/devtools?utm_source=devtools)
also from debugger
Upvotes: 7
Reputation: 1
Add this to your console and execute
copy(JSON.stringify(foo));
This copies your JSON to clipboard
Upvotes: -1
Reputation: 107
Right click on data which you want to store
Upvotes: 0
Reputation: 325
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;
}, ' ');
}
Upvotes: 1
Reputation: 3823
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));
Upvotes: 45
Reputation: 10736
If you've sent the object over a request you can copy it from the Chrome -> Network tab.
Request Payload - > View Source
Upvotes: 14
Reputation: 8571
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
Upvotes: 26
Reputation: 750
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.
Upvotes: 0
Reputation: 3701
Follow the following steps:
JSON.stringify(temp1)
.Upvotes: 13
Reputation: 20851
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.
Upvotes: 0