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

How to use js to modify the content in a certain json?

Guide ,
Jun 25, 2025 Jun 25, 2025

For example:

There are two files my.json and aa.jsx in the same path.

I have a code in my.json that looks like this:
{
"oldCode": "60068"
}

 

I now want to use var changeCode="10020" in aa.jsx to change the value of oldCode.

How to achieve it?

Thank you very much.

TOPICS
Scripting
1.4K
Translate
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 , Jun 25, 2025 Jun 25, 2025

Hi @dublove have a study of this:

/**
 * Example of reading, parse and writing JSON file.
 *
 * @author m1b
 * @version 2025-06-25
 * @discussion https://community.adobe.com/t5/indesign-discussions/how-to-use-js-to-modify-the-content-in-a-certain-json/m-p/15387727
 */
//@include '../../../../Lib/json3.js'
(function () {

    // read the json file's contents
    var myJSON = readFile('my.json');

    if (!myJSON)
        return alert('Could not read JSON file.');

    // parse as json
    myJSON 
...
Translate
Guide ,
Jun 27, 2025 Jun 27, 2025

test.jsx just adds the modified oldCode code you gave me.
The rest is just a test.

 

Create PDF, here is just a test, I still mainly want to modify the oldCode in my.json

Translate
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
Guide ,
Jun 27, 2025 Jun 27, 2025

The new one, it seems, is much harder to understand and not easy to interface with other js.

It's a straight tip:

dublove_0-1751022842441.jpeg

 

Translate
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
Guide ,
Jun 27, 2025 Jun 27, 2025

Hi m1b.

There should be an error in the file.
I tested file and redefined it and there was no prompt, but of course it was an error and no result.
I don't know how to correct it.

var file = new File(pathToMyJSONFile);
file.open();
file.encoding = "UTF-8";
success = file.write(bom + data);
file.close();
Translate
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
Guide ,
Jun 28, 2025 Jun 28, 2025
LATEST

Hi m1b.

The problem seems to be solved, I didn't use this new one you posted, this one I couldn't read.
The problem is:
my.JSON path can not have Chinese characters.
And my.JSON file can not have comments, blank lines!

Translate
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
Engaged ,
Jun 25, 2025 Jun 25, 2025

Hey @dublove 

From what I read, I understand you have a script that is populated by a Json file. So far so good.

The way I'm using them, Json files are settings files that are created/ updated via .jsx files (ie scripts).

 

My question would then be : Is it the case here? Is your json file created by your .jsx script, or is it just reading datas from it? If so, how was the Json file created in the first place?

 

The usual way would be to run the script / locate the edittext  where value needs to be changed / change it / save it.

From there, the json file should be update with the new value so then when you run the script the next time, it would be with the updated value.

 

I'm afraid I can't help more with the little information provided ^^

Translate
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
Guide ,
Jun 25, 2025 Jun 25, 2025

Hi  Fred.L

The json is created manually.
I don't really understand it either.

Translate
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