• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
5

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

Community Beginner ,
Jul 18, 2023 Jul 18, 2023

Copy link to clipboard

Copied

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?

TOPICS
How to , Scripting , UXP Scripting

Views

1.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jul 18, 2023 Jul 18, 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

Votes

Translate

Translate
Community Expert ,
Jul 18, 2023 Jul 18, 2023

Copy link to clipboard

Copied

Does this work?

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 18, 2023 Jul 18, 2023

Copy link to clipboard

Copied

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.
});

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 18, 2023 Jul 18, 2023

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 18, 2023 Jul 18, 2023

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 18, 2023 Jul 18, 2023

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 18, 2023 Jul 18, 2023

Copy link to clipboard

Copied

I moved the JSON file to the same dir as the script and can now load it using require. However, when I try to process the script, it throws an "object invalid" error. I also tried to apply JSON.parse on the loaded file, but that gives the same error as the original issue, "Unexpected token o in position 1".

 

I'm going to give up on this one and simply paste the JSON into the script. I don't have the time currently to trouble shoot this.

 

Thanks for looking into this and giving suggestions.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 18, 2023 Jul 18, 2023

Copy link to clipboard

Copied

I mistakenly deleted some files and that's why I was getting the error. You're require solution does work. I'll mark it as correct since it's the closest to what I was trying to accomplish.

 

Thanks for the help.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 21, 2023 Jul 21, 2023

Copy link to clipboard

Copied

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.)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 22, 2023 Jul 22, 2023

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 22, 2023 Jul 22, 2023

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 22, 2023 Jul 22, 2023

Copy link to clipboard

Copied

LATEST

Oh I meant to further the discussion by affirming my findings. As if all the scenarios discussed here are true then surely something is broken in UXP implementation in this respect. If security was of consideration then require is bypassing it, and then again even the file is read albeit wrongly.

-Manan

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines