'Copying variable contents to clipboard while debugging in Visual Studio Code
I'm debugging in Visual Studio Code and I have a JSON object that I would like to copy as text to the clipboard.
Is this possible inside of Visual Studio Code?
Solution 1:[1]
I found two ways to do that, both of which are a bit hacky (in my eyes).
Use console.log
I think there will be a limit to the size of the string that this can output, but it was satisfactory for my requirements.
- In the debug console, write
console.log(JSON.stringify(yourJsonObject)) - Copy the resulting output from the debug console. That can be a bit tedious for long strings, but a combination of mouse and keyboard (ctrl-shift-end) worked ok for me.
Use a watch (limited to 10'000 characters)
This method only works up to a limited size of the resulting json string (it looks like 10'000 characters).
- Set a breakpoint in a reasonable location where your variable is in scope and start your app.
- Go to the debug view, add a watch for a temporary variable, e.g.
tmpJson - Get your breakpoint to hit.
- In the debug console, write
var tmpJson = JSON.stringify(yourJsonObject) - This will now have populated the watched variable
tmpJsonwith the string representation of your json object - In the debug view, right click on the watched variable, click copy.
If the string is too long, it cuts it off with a message like the following:
...,"typeName":"rouParallel","toolAssembly":{"id":"ASKA800201","description":"CeonoglodaloD50R6z5","c... (length: 80365)"
But it would work for smaller objects. Maybe this helps some people. It would be great to have this properly built-in with vscode.
Solution 2:[2]
There is an open issue regarding this: https://github.com/microsoft/vscode-java-debug/issues/624
Workaround :
- Go to the VARIABLES panel and right click to show contextual menu on a variable
- select Set Value
- Ctrl+C
(tested on Java, not JavaScript)
Solution 3:[3]
I have an easy workaround to copy anything you want:
- In the debug console, write
JSON.stringify(yourJsonObject) - Copy the string without the double quotes
"around the string Open a browser, such as Chrome, open the inspecting tool, go on the console and write:
copy(JSON.parse("PASTE_THE_STRING_HERE"));The object is now copy on your keyboard !
Solution 4:[4]
Tested in python debugger
Add the variable to Watch, but converted to string
str(myvar)Right-click on the value of the watch, and select
Copy ValueNow you should get the full value, even for very long values
- This answer suggests a 10,000 char limit and uses JSON.stringify(myvar) instead of just str(). On char limit, see also this comment below.
(var name blurred out):
Solution 5:[5]
If you are debugging Python:
In the DEBUG CONSOLE type, for example:
import json
from pprint import pprint as pp
pp(json.dumps(outDetailsDict))
OUTPUT IS LIKE
{"": {"stn_ix": 43, "stn_name": "Historic Folsom Station (WB)", "name": "", },
...
Solution 6:[6]
The fastest way I found to do that on Visual Studio Code was
- Adding a breakpoint where is located the object to copy
- Right click on object and choose "Add to Watch"
- From Watch sidebar, choose option "Copy Value" and it's all! ?
Solution 7:[7]
If you're in debug mode, you can copy any variable by writing copy() in the debug terminal.
This works with nested objects and also removes truncation and copies the complete value.
Tip: you can right click a variable, and click Copy as Expression and then paste that in the copy-function.
Solution 8:[8]
While the question presumably deals with JavaScript (JSON) based technologies, many people have posted Python-related posts in this thread. So I'll post a more specific answer for Python, although the reasoning can be extended with some tweaks to JavaScript-based technologies. ?
Helper strategies for debugging with VSCode
Copying variable FULL VALUE (not truncated) to clipboard while debugging even for very long values and other additional strategies.
1 APPROACH: Access the "Run and Debug" screen and look for the "WATCH" area and double click inside it. Add something like str(<MY_VAR>) which should be the variable you want to find the value of during the debug process;
2 APPROACH: Access the "DEBUG CONSOLE" tab and in the footer (symbol ">") copy and paste the code below or another one of your choice...
def print_py_obj(py_obj, print_id="print_py_obj"):
"""Prints a Python object with its string, type, dir, dict, etc...
Args:
py_obj (Any): Any Python object;
print_id (Optional[str]): Some identifier.
"""
print(" " + str(print_id) + \
" DEBUG >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
print(" 0 STR >>>>>>>>>>>>>>>>>>>>>>>>")
print(str(py_obj))
print(" 0 <<<<<<<<<<<<<<<<<<<<<<<<")
print(" 1 TYPE >>>>>>>>>>>>>>>>>>>>>>>>")
print(type(py_obj))
print(" 1 <<<<<<<<<<<<<<<<<<<<<<<<")
print(" 2 DIR >>>>>>>>>>>>>>>>>>>>>>>>")
print(str(dir(py_obj)))
print(" 2 <<<<<<<<<<<<<<<<<<<<<<<<")
print(" 3 DICT >>>>>>>>>>>>>>>>>>>>>>>>")
try:
print(vars(py_obj))
except Exception:
pass
print(" 3 <<<<<<<<<<<<<<<<<<<<<<<<")
print(" " + str(print_id) + \
" DEBUG <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
print_py_obj(profit, "<MY_VAR_OPTIONAL_IDENTIFIER>")
NOTE: We may have a 10,000 character limitation for both approaches.
Thanks! ???
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ben |
| Solution 2 | Paul Roub |
| Solution 3 | Emidomenge |
| Solution 4 | |
| Solution 5 | Love and peace - Joe Codeswell |
| Solution 6 | Jonathan Brizio |
| Solution 7 | |
| Solution 8 |

