Is there a way to save data as json file or export to json?

Is there a way to save data as json file or export to json in Playcanvas to playcanvas project?
I see these ways: to save data to localStorage then copy data and save locally as json and upload to playcanvas project, use 3rd party library but what else?

Hi @grzesiekmq,

To save programmatically an asset to your Playcanvas project you can use the Rest API, specifically the Create asset call:

https://developer.playcanvas.com/en/user-manual/api/asset-create/

1 Like

ok but how to do this?
pc.http.post('https://playcanvas.com/api/assets', this.arrPos, null, (err, response) => console.log(response));
/assets/
and there name, projectId, file encoded as query string with ? char
dont want from console like from cmd or Powershell but from app
oh there should be array in object I mean {[]}

Depending on how you have done this, I would save the data in memory (rather than localStorage) and just save it out as a JSON file. eg https://stackoverflow.com/questions/19721439/download-json-object-as-a-file-from-browser

When it’s a JSON file, you can just upload it as is to the PlayCanvas Editor project.

2 Likes

ok how to access the html data?
I try

const dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(obj));
const dlAnchorElem = this.exportJSON.resource.getElementById('downloadAnchorElem');
dlAnchorElem.setAttribute("href",     dataStr     );
dlAnchorElem.setAttribute("download", "bumps.json");
dlAnchorElem.click();

is it required all html like <html><head><body> etc. or only <a id="downloadAnchorElem" style="display:none"></a>

?

this.exportJSON.resource.getElementById is not a function

this.exportJSON is script attribute type asset html
I’m confused because I’m aquianted with document.getElementById()

not clicking for export

const dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(obj));
const doc = new DOMParser();
const html = doc.parseFromString(this.exportJSON.resource, "text/html");
console.log(html);
const dlAnchorElem = html.getElementById('downloadAnchorElem');
dlAnchorElem.setAttribute("href",     dataStr     );
dlAnchorElem.setAttribute("download", "bumps.json");
// works to this place
dlAnchorElem.click();

Here’s a thread and post to an example project of downloading a CSV file [SOLVED] List with rows cannot be accessed by common JS syntax; '.data'

If you are still having trouble, create a new small project that shows the error that you getting and the logic you are trying to implement.

1 Like