Copy link to clipboard
Copied
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.
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 ...
Copy link to clipboard
Copied
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 = JSON.parse(myJSON);
alert('oldCode = ' + myJSON.oldCode);
// change the json
myJSON.oldCode = Math.floor(Math.random() * 999999);
// write the json file
var f = writeFile('my.json', JSON.stringify(myJSON, undefined, '\t'));
})();
/**
* Read a file and return contents.
* Will ask for file if no path is supplied.
* @author m1b
* @version 2023-08-17
* @param {File|CSFile|String} [file] - file or path.
* @param {String} [fileExtension] - the file extension (default: '').
* @param {Number} [lineCount] - the number of lines to read (default: all).
* @returns {String} file contents.
*/
function readFile(file, fileExtension, lineCount) {
fileExtension = (fileExtension || '').replace('.', '');
lineCount = lineCount || 0;
var f,
data = '';
if (file != undefined) {
if (file.constructor.name == 'String') {
if (file.indexOf('/') < 0)
file = File($.fileName).parent + '/' + file;
f = File(file);
}
else if (file.constructor.name == 'File')
f = file;
}
if (
f == undefined
|| typeof f.open !== 'function'
)
f = Utility.chooseFile(fileExtension, false, file);
if (!f || !f.exists)
return;
$.appEncoding = 'UTF-8';
f.open('r');
if (lineCount == 0)
// read whole file
data = f.read();
else
// read first line(s)
while (lineCount--)
data += f.readln() + '\n';
f.close();
if (data == undefined)
throw Error('readFile: Could not read data.');
return data;
};
/**
* Write text to file.
* @author m1b
* @version 2023-05-30
* @param {File|String} file - the file|path to write to.
* @param {String} data - the data to write.
* @param {Boolean} append - whether to append to end of file (default: false).
* @returns {File} the written file
*/
function writeFile(file, data, append) {
var success,
writeMode = append == true ? 'a' : 'w',
bom = "\uFEFF";
if (file.constructor.name == 'String') {
if (file.indexOf('/') < 0)
file = File($.fileName).parent + '/' + file;
file = File(file);
}
try {
file.open(writeMode);
file.encoding = "UTF-8";
success = file.write(bom + data);
file.close();
}
catch (error) {
success = false;
alert('writeFile failed: ' + error.message);
}
if (success)
return file;
};
Note: this script uses the json3.js library. It won't work without it (or a similar library).
Copy link to clipboard
Copied
Hi m1b.
Thnak you very much.
The script works fine.
You're really, really amazing.
How did you know I needed a random number?
I was about to ask another question:
Random Match.
Copy link to clipboard
Copied
You're really, really amazing.
How did you know I needed a random number?
Well, you just said it!
He knew because he's amazing. What else? ^^
Copy link to clipboard
Copied
Thank you.
Copy link to clipboard
Copied
Haha, I wrote the random number just so that every time you run it it changes the json value, so you can be sure the script is working! Amazing... coincidence! 🙂
Copy link to clipboard
Copied
I now use both
//@include '... /PubLib-public-library/json.jsx';
//@include '. /PubLib-public-library/json3.jsx';
It will say JSON.eval is undefined
function getJson() {
myJSONPath = File($.fileName).parent.parent + '/PubLib-公供库/my.json';
if (!myJSONPath)
alert('Failed to load JSON.');
myJSONObject = evalJSON(myJSONPath);
}
function readFile(path) {
var file = File(path);
if (!file.exists)
return;
file.open('r');
var content = file.read();
file.close();
return content;
};
function evalJSON(path) {
json = readFile(path);
if (!json)
return;
return JSON.eval(json);
};They seem to conflict, can I just use onlyone?
Copy link to clipboard
Copied
Speechless, it's only been one night and it's not measuring right again.
Murphy has a ghost.
I can't believe it's prompting an error with json3.jsx.
Copy link to clipboard
Copied
Yeah you can't use both. Just use one. Use json3 unless you understand the other library better (I don't know it). json3 (and json2 is the same) has methods: parse and stringify. I showed you how to use them in my script above.
- Mark
P.S. the reason you can't use both is that they instantiate as the same named object "JSON". If you load them both, then only the last one will be available. That is why JSON.eval gives the error—because json3.js loaded last and it doesn't have an eval method.
Copy link to clipboard
Copied
If you only use json3.js, the above function getJson() does not seem to work.
Copy link to clipboard
Copied
That's correct, because json3 doesn't know the method JSON.eval.
Try this:
function parseJSON(path) {
var json = readFile(path);
if (!json)
return;
try {
return JSON.parse(json);
}
catch (error) {
alert('Failed to parse JSON (' + error.message + ')';
}
};
- Mark
Copy link to clipboard
Copied
I just replaced all the JSON in json.jsx with JSONONE.
It works surprisingly.
Copy link to clipboard
Copied
It didn't work.
I'll think of a way to keep the two from meeting.
Copy link to clipboard
Copied
It's a bad idea and a waste to use both, because they both do the same thing.
Better to find out what the error is. Try the parseJSON function that I just posted. I know you aren't calling it because my function will give a different error message. Note that my function is called "parseJSON" not "evalJSON", so you need to make sure that the correct function is called elsewhere in your script.
- Mark
Copy link to clipboard
Copied
ro here
I made a sample, can you test it for me.
Now, it can create PDF path to the desktop.Could you please see how to change the value of “oldcode” in my.json?.
Usage, please see Notepad: create a shortcut to the main Scripts Panel path can be.
Oddly enough, the following doesn't work.
function getJson() {
var myJSONPath = File($.fileName).parent.parent + '/PubLib/my.json';
var myJSONObject = evalJSON(myJSONPath);
}Thank you very much!
Copy link to clipboard
Copied
@dublove I had a look at your zip file.
The Test.jsx file should just be this:
/*
Change image Link format.
Thanks to rob day m1b
https://community.adobe.com/t5/indesign-discussions/the-dialog-box-still-won-t-how-to-combine-these-3-in-a-dropdown-list/m-p/15381260#M628897
*/
//@include '../PubLib/pubLib.jsx';
//@include '../PubLib/json3.jsx';
(function () {
//Getting the contents of a Json
var pathToMyJSONFile = File($.fileName).parent.parent + '/PubLib/my.json';
var myJSONObject = parseJSON(pathToMyJSONFile);
if (!myJSONObject)
return alert('Could not load json file (' + pathToMyJSONFile + ').');
var oldCode = myJSONObject.oldCode;
var exPDFname = myJSONObject.exPDFname;
createFolder(exPDFname);
alert("Created: " + exPDFname + " folder on Desktop");
})();
The PubLib.jsx file should be just this:
// read a text file
function readFile(path) {
var file = File(path);
if (!file.exists)
return;
file.open('r');
var content = file.read();
file.close();
return content;
};
// parse a json file
function parseJSON(path) {
var json = readFile(path);
if (!json)
return;
try {
return JSON.parse(json);
}
catch (error) {
alert('Failed to parse JSON (' + error.message + ')');
}
};
function writeFile(file, data, append) {
var success,
writeMode = append == true ? 'a' : 'w',
bom = "\uFEFF";
if (file.constructor.name == 'String')
file = File(file);
try {
file.open(writeMode);
file.encoding = "UTF-8";
success = file.write(bom + data);
file.close();
}
catch (error) {
success = false;
alert('writeFile failed: ' + error.message);
}
if (success)
return file;
};
// write an object to file as json string
function writeJSON(file, obj) {
if ('String' === file.constructor.name)
file = File(file);
try {
// convert object to json string
var json = JSON.stringify(obj, undefined, '\t');
} catch (error) {
return alert('Failed to stringify object.');
}
var jsonFile = writeFile(file, json);
return jsonFile
};
// create a folder
function createFolder(folderName) {
var SCRIPTS_FOLDER = decodeURI(Folder.desktop);
var folder1 = Folder(SCRIPTS_FOLDER + "/" + folderName);
//Check if it exist, if not create it.
if (!folder1.exists)
folder1.create();
if (!folder1.exists)
alert("You need to run this script as Administrator\nThe folder was NOT created");
};
Edit 2025-06-27: added more functions to PubLib
Copy link to clipboard
Copied
I have been able to read the my.json file.
alert(myJSON); shows up as it does on the left.
But strangely, it says that the following line is wrong:
myJSON = JSON.parse(myJSON);
Copy link to clipboard
Copied
This means you have the wrong JSON object in memory.
This will check:
if ('undefined' === typeof JSON)
alert('✗ No JSON library is loaded.');
else if ('function' === typeof JSON.eval)
alert('✗ Wrong JSON library is loaded.');
else if ('function' === typeof JSON.parse)
alert('✓ Correct JSON library is loaded.');
Copy link to clipboard
Copied
Hi m1b.
Can your test modify my.json?
Everything can be caused by path irregularities.
I've tried it on the same path with no problem.
Now this pops up. No problem in the front (Correct JSON library is loaded)
try {
file.open(writeMode);
file.encoding = "UTF-8";
success = file.write(bom + data);
file.close();
}
Copy link to clipboard
Copied
> WriteFile failed: file.open is not a function
This error is probably because you are trying to open a String (a path), rather than a File.
I have added two more functions to your PubLib listing above. When you make the change to your object you can use
writeJSON(myJSONFile, myObject);
- Mark
Copy link to clipboard
Copied
Now it seems to be possible to read my.json with json3.jsx (It should be able to do without json.jsx in the future).
I added the original problem of this post: Try to changing the value of "oldCode" in my.json .
Still reporting the same error
writeFile failed:file.open is not a function!
Also now the variables in publib.jsx can't be passed to text.jsx, so I had to cancel the "var".
You try it and see.
Copy link to clipboard
Copied
I feel. The key is still that issue json.jsx can't be shared with json3.jsx.
Instead of using json3.jsx, is it possible to modify oldCode in other ways?
Copy link to clipboard
Copied
> I feel. The key is still that issue json.jsx can't be shared with json3.jsx.
You must only use one json library. Don't worry about that.
> Instead of using json3.jsx, is it possible to modify oldCode in other ways?
There are other ways, but we are going to do it the best way, using a json library.
Copy link to clipboard
Copied
I've changed it to this now.
It still prompts: writeFile failed:file.open is not a function!
publib.jsx
// read a text file
function getJson() {
//Getting the contents of a Json
pathToMyJSONFile = File($.fileName).parent.parent + '/PubLib/my.json';
myJSONObject = parseJSON(pathToMyJSONFile);
if (!myJSONObject)
return alert('Could not load json file (' + pathToMyJSONFile + ').');
oldCode = myJSONObject.oldCode;
exPDFname = myJSONObject.exPDFname;
}
function readFile(path) {
var file = File(path);
if (!file.exists)
return;
file.open('r');
var content = file.read();
file.close();
return content;
};
// parse a json file
function parseJSON(path) {
var json = readFile(path);
if (!json)
return;
try {
return JSON.parse(json);
}
catch (error) {
alert('Failed to parse JSON (' + error.message + ')');
}
};
function writeFile(file, data, append) {
var success,
writeMode = append == true ? 'a' : 'w',
bom = "\uFEFF";
if (file.constructor.name == 'String')
file = File(file);
try {
file.open(writeMode);
file.encoding = "UTF-8";
success = file.write(bom + data);
file.close();
}
catch (error) {
success = false;
alert('writeFile failed: ' + error.message);
}
if (success)
return file;
};
// write an object to file as json string
function writeJSON(file, obj) {
if ('String' === file.constructor.name)
file = File(file);
try {
// convert object to json string
var json = JSON.stringify(obj, undefined, '\t');
} catch (error) {
return alert('Failed to stringify object.');
}
var jsonFile = writeFile(file, json);
return jsonFile
};
// create a folder
function createFolder(folderName) {
var SCRIPTS_FOLDER = decodeURI(Folder.desktop);
var folder1 = Folder(SCRIPTS_FOLDER + "/" + folderName);
//Check if it exist, if not create it.
if (!folder1.exists)
folder1.create();
if (!folder1.exists)
alert("You need to run this script as Administrator\nThe folder was NOT created");
};
my test.jsx
/*
Change image Link format.
Thanks to rob day m1b
https://community.adobe.com/t5/indesign-discussions/the-dialog-box-still-won-t-how-to-combine-these-3-in-a-dropdown-list/m-p/15381260#M628897
*/
//@include '../PubLib/pubLib.jsx';
//@include '../PubLib/json3.jsx';
getJson();
createFolder(exPDFname);
alert("Created: " + exPDFname + " folder on Desktop");
readCheck();
//read and Check oldCode
function readCheck() {
var myJSON = readFile(pathToMyJSONFile);
if (!myJSON)
return alert('Could not read JSON file.');
//alert("a2:" + myJSON);
// parse as json
myJSON = JSON.parse(myJSON);
alert('oldCode = ' + myJSON.oldCode);
// change the json
myJSON.oldCode = Math.floor(Math.random() * 999999);
//alert(myJSON.oldCode);
// write the json file
var f = writeFile(myJSON, JSON.stringify(myJSON, undefined, '\t'));
}
/**
* Read a file and return contents.
* Will ask for file if no path is supplied.
* @author m1b
* @version 2023-08-17
* @Param {File|CSFile|String} [file] - file or path.
* @Param {String} [fileExtension] - the file extension (default: '').
* @Param {Number} [lineCount] - the number of lines to read (default: all).
* @Returns {String} file contents.
*/
function readFile(file, fileExtension, lineCount) {
fileExtension = (fileExtension || '').replace('.', '');
lineCount = lineCount || 0;
var f,
data = '';
if (file != undefined) {
if (file.constructor.name == 'String') {
if (file.indexOf('/') < 0)
file = File($.fileName).parent + '/' + file;
f = File(file);
}
else if (file.constructor.name == 'File')
f = file;
}
if (
f == undefined
|| typeof f.open !== 'function'
)
f = Utility.chooseFile(fileExtension, false, file);
if (!f || !f.exists)
return;
$.appEncoding = 'UTF-8';
f.open('r');
if (lineCount == 0)
// read whole file
data = f.read();
else
// read first line(s)
while (lineCount--)
data += f.readln() + '\n';
f.close();
if (data == undefined)
throw Error('readFile: Could not read data.');
return data;
};
/**
* Write text to file.
* @author m1b
* @version 2023-05-30
* @Param {File|String} file - the file|path to write to.
* @Param {String} data - the data to write.
* @Param {Boolean} append - whether to append to end of file (default: false).
* @Returns {File} the written file
*/
function writeFile(file, data, append) {
var success,
writeMode = append == true ? 'a' : 'w',
bom = "\uFEFF";
//if ('undefined' === typeof JSON)
// alert('✗ No JSON library is loaded.');
//else if ('function' === typeof JSON.eval)
// alert('✗ Wrong JSON library is loaded.');
//else if ('function' === typeof JSON.parse)
// alert('✓ Correct JSON library is loaded.');
if (file.constructor.name == 'String') {
if (file.indexOf('/') < 0)
file = File($.fileName).parent + '/' + file;
file = File(file);
}
//alert("a3:" + file);
//alert(file.oldCode);
try {
file.open(writeMode);
file.encoding = "UTF-8";
success = file.write(bom + data);
file.close();
}
catch (error) {
success = false;
alert('writeFile failed: ' + error.message);
}
if (success)
return file;
};
Copy link to clipboard
Copied
@dublove you are getting into a bit of a mess.
This is what test.jsx should be:
/*
Change image Link format.
Thanks to rob day m1b
https://community.adobe.com/t5/indesign-discussions/the-dialog-box-still-won-t-how-to-combine-these-3-in-a-dropdown-list/m-p/15381260#M628897
*/
// must load json3 before pubLib
//@include '../PubLib/json3.jsx';
//@include '../PubLib/pubLib.jsx';
getJson();
createFolder(exPDFname);
alert("Created: " + exPDFname + " folder on Desktop");
var pathToJSONFile = File($.fileName).parent.parent + '/PubLib/my.json';
readCheck(pathToJSONFile);
//read and Check oldCode
function readCheck(path) {
var myJSON = readFile(path);
if (!myJSON)
return alert('Could not read JSON file.');
//alert("a2:" + myJSON);
// parse as json
myJSON = JSON.parse(myJSON);
alert('oldCode = ' + myJSON.oldCode);
// change the json
myJSON.oldCode = Math.floor(Math.random() * 999999);
//alert(myJSON.oldCode);
// write the json file
var f = writeFile(File(path), JSON.stringify(myJSON, undefined, '\t'));
};
This is what pubLib.jsx should be:
if ('undefined' === typeof JSON)
throw new Error('✗ No JSON library is loaded.');
else if ('function' !== typeof JSON.parse)
throw new Error('✗ Wrong JSON library is loaded.');
// get the contents of a json file
function getJson(path) {
myJSONObject = parseJSON(path);
if (!myJSONObject)
return alert('Could not load json file (' + path + ').');
oldCode = myJSONObject.oldCode;
exPDFname = myJSONObject.exPDFname;
};
// parse a json file
function parseJSON(path) {
var json = readFile(path);
if (!json)
return;
try {
return JSON.parse(json);
}
catch (error) {
alert('Failed to parse JSON (' + error.message + ')');
}
};
/**
* Read a file and return contents.
* Will ask for file if no path is supplied.
* @author m1b
* @version 2023-08-17
* @Param {File|CSFile|String} [file] - file or path.
* @Param {String} [fileExtension] - the file extension (default: '').
* @Param {Number} [lineCount] - the number of lines to read (default: all).
* @Returns {String} file contents.
*/
function readFile(file, fileExtension, lineCount) {
fileExtension = (fileExtension || '').replace('.', '');
lineCount = lineCount || 0;
var f,
data = '';
if (file != undefined) {
if (file.constructor.name == 'String') {
if (file.indexOf('/') < 0)
file = File($.fileName).parent + '/' + file;
f = File(file);
}
else if (file.constructor.name == 'File')
f = file;
}
if (!f || !f.exists)
return;
$.appEncoding = 'UTF-8';
f.open('r');
if (lineCount == 0)
// read whole file
data = f.read();
else
// read first line(s)
while (lineCount--)
data += f.readln() + '\n';
f.close();
if (data == undefined)
throw Error('readFile: Could not read data.');
return data;
};
/**
* Write text to file.
* @author m1b
* @version 2023-05-30
* @Param {File|String} file - the file|path to write to.
* @Param {String} data - the data to write.
* @Param {Boolean} append - whether to append to end of file (default: false).
* @Returns {File} the written file
*/
function writeFile(file, data, append) {
var success,
writeMode = append == true ? 'a' : 'w',
bom = "\uFEFF";
if (file.constructor.name == 'String')
file = File(file);
try {
file.open(writeMode);
file.encoding = "UTF-8";
success = file.write(bom + data);
file.close();
}
catch (error) {
success = false;
alert('writeFile failed: ' + error.message);
}
if (success)
return file;
};
// write an object to file as json string
function writeJSON(file, obj) {
if ('String' === file.constructor.name)
file = File(file);
try {
// convert object to json string
var json = JSON.stringify(obj, undefined, '\t');
} catch (error) {
return alert('Failed to stringify object.');
}
var jsonFile = writeFile(file, json);
return jsonFile
};
// create a folder
function createFolder(folderName) {
var SCRIPTS_FOLDER = decodeURI(Folder.desktop);
var folder1 = Folder(SCRIPTS_FOLDER + "/" + folderName);
//Check if it exist, if not create it.
if (!folder1.exists)
folder1.create();
if (!folder1.exists)
alert("You need to run this script as Administrator\nThe folder was NOT created");
};
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more