Skip to main content
Inspiring
July 18, 2023
Answered

How do you correctly load a JSON file from disk in a UXP script?

  • July 18, 2023
  • 3 replies
  • 2166 views

I'm not a JavaScript or UXP expert, most likely I'm doing something obvious wrong here. But I haven't been able to find an answer for this anywhere.

 

Here's how I attempt to load the JSON:

 

const fs = require('fs');
const data = JSON.parse(fs.readFile("path/to/file.json"));

 

However, the last line gives the error "Unexpected token o in position 1" and the script quits.

 

I have double-checked the file path and made sure it is correct. Also, if I copy/paste the JSON directly into the script everything works correctly, so I know the file is not corrupt.

 

What is wrong here?

This topic has been closed for replies.
Correct answer Manan Joshi

What @Eugene Tyson suggested regarding using the sync variant of the method or parsing the content in the callback are valid suggestions. However, with this done I still get the error you say. Checking the content of the file read I see that all the double quotes are escaped.

I suggest another way to read the json file. Just require it and you should be good.

 

const data = require(path to the JSON file)

P.S. :- Make sure the path is relative to your script file

 

-Manan

3 replies

Brainiac
July 22, 2023

It has already been solved, but I will explain.

 

UXP plugins can use fs by giving permissions, but UXP scripts cannot for security reasons. Instead, call localFileSystem. In this case, the user must select the file through a dialog. So I am surprised that you can read JSON using require.

 

const { localFileSystem } = require('uxp').storage ;

const targetEntry = await localFileSystem.getFileForOpening({types: ['json']}) ;
if(targetEntry == null) {return ;}

const data = JSON.parse(await targetEntry.read()) ;

 

(Edit: UXP scripts could use the fs module without permissions.)

Adobe Expert
July 23, 2023

Hi @sttk3,

I have tested and the OP as well that the require is able to read the file. Secondly even fs is able to read the file, but as far as I remember it was escaping the double quotes. Lastly, thanks for sharing another way to do this but I would say this ain't a proper replacement of what the OP might be wanting to do, with scripts we many times have use case of silent execution in such cases making selection on a dialog would be disruptive.

-Manan

Brainiac
July 23, 2023

I don’t mean to deny what you said, just that I was pleasantly surprised you found a way to make it work with UXP’s limited file access.

Manan JoshiCorrect answer
Adobe Expert
July 19, 2023

What @Eugene Tyson suggested regarding using the sync variant of the method or parsing the content in the callback are valid suggestions. However, with this done I still get the error you say. Checking the content of the file read I see that all the double quotes are escaped.

I suggest another way to read the json file. Just require it and you should be good.

 

const data = require(path to the JSON file)

P.S. :- Make sure the path is relative to your script file

 

-Manan

satorisAuthor
Inspiring
July 19, 2023

Using require with the path to the json file gives the error "Module not found". I thought require was only for modules, or is there some sort of hack to get around that? From the sound of what you're saying, it appears that this is a bug. Does Adobe have a portal somewhere to report it, or do they look here?

 

For now, I'll just continue pasting in the json into the script. It's not ideal, but at least I can get things done.

Adobe Expert
July 19, 2023

It's not a bug reading file using require. I was able to read the file fine usin require. Did you use the path relative to the script?

https://stackoverflow.com/questions/7163061/is-there-a-require-for-json-in-node-js

The other use case that we were trying to solve using readFile is still not working but I am not sure if it's a bug or are we missing something typical to UXP

-Manan

Adobe Expert
July 19, 2023

Does this work?

const fs = require('fs');
const data = JSON.parse(fs.readFileSync("path/to/file.json", 'utf8'));
Adobe Expert
July 19, 2023

Or maybe this

 

const fs = require('fs');
fs.readFile("path/to/file.json", 'utf8', (err, data) => {
  if (err) {
    console.error('Error reading the file:', err);
    return;
  }
  
  const jsonData = JSON.parse(data);
  // Now you can work with the jsonData object here.
});