0

I am using below plugin to show json .

https://www.npmjs.com/package/react-json-view.

can we have a button to which will copy this json.

https://codesandbox.io/s/async-pine-o6eho

return (
    <div className="App">
      <button
        onClick={() => {
          alert("---");
        }}
      >
        Copy
      </button>
      <ReactJson src={my_json_object} theme="monokai" />
    </div>
  );

can we copy the below json output on button click?

  • Where is the "below json"? And what do you mean "copy"? – Dexygen Nov 23 '19 at 06:21
  • if you open `conpen` .it will show `json viewer` –  Nov 23 '19 at 06:39
  • @copy means `on button` click it will copy this value and paste in notpad –  Nov 23 '19 at 06:40
  • Your problem solution is here, please try this https://stackoverflow.com/questions/11849562/how-to-save-the-output-of-a-console-logobject-to-a-file thanks! – Ericgit Nov 23 '19 at 07:16

2 Answers2

2

You can copy your JSON by setting content of it to a temporary input and using document.execCommand('copy').

function Copy(json) {
    const input = document.createElement("textarea");
    document.body.appendChild(input);
    input.value = JSON.stringify(json);
    input.select();
    document.execCommand("copy");
    document.body.removeChild(input);
}

Here is a working example based on your code.

N'Bayramberdiyev
  • 5,936
  • 7
  • 27
  • 47
0

Try this...

onClick={() => {

  let objCopy = Object.assign({}, my_json_object);
  alert(JSON.stringify(objCopy));
}}