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

How to transfer myparStn to n in myJSON.n = v;, changing it to myJSON.myparStn = v;

Guide ,
Aug 06, 2025 Aug 06, 2025

I want to reuse the rwJSON() function, but I don't know how to pass the name to "myJSON. n = v";
For example, I want to write data with a field (name is myParStn, value is 20) to my.json, but I found that rwJSON(myPar, v) cannot be implemented.

 

Here, n cannot be used as a parameter.
It can be used as a field name on its own.

 

rwJSON(myparStn,r);
function rwJSON(n,v) {
    // read the json file's contents
    var myJSON = readFile(pathToMyJSONFile);
    if (!myJSON)
        return alert('Could not read JSON file.');
    // parse as json
    myJSON = JSON.parse(myJSON);
    // input text;
    myJSON.n = v;   
    var f = writeFile(pathToMyJSONFile, JSON.stringify(myJSON, undefined, '\t'));
}

 

TOPICS
How to , Scripting
336
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 , Aug 08, 2025 Aug 08, 2025

Try the following

var abc="111";
rwJSON("myparStn",abc);

function rwJSON(n,v) {
    // read the json file's contents
    var myJSON = readFile(pathToMyJSONFile);
    if (!myJSON)
        return alert('Could not read JSON file.');
    // parse as json
    myJSON = JSON.parse(myJSON);
    // input text;
    //myJSON.n = v;   
     myJSON[n] = v;  
    var f = writeFile(pathToMyJSONFile, JSON.stringify(myJSON, undefined, '\t'));
}

-Manan

Translate
Guide ,
Aug 06, 2025 Aug 06, 2025

Come on, all kinds of strange and absurd attempts.

No, I need to change my thinking.

 myJSON.{[n\} = r;
  myJSON.[(n)] = r;

 

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
Community Expert ,
Aug 07, 2025 Aug 07, 2025

Give the JSON within which you want to write and then we shall try. Just looking at the code is one aspect we don't know what structure your JSON has and what you are trying to add into it

-Manan

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
Community Expert ,
Aug 07, 2025 Aug 07, 2025

If the following line cause an error for you

myJSON.n = v; 

Try the following 

myJSON[n] = v;

 -Manan

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 ,
Aug 07, 2025 Aug 07, 2025

Hi @Manan Joshi 

Sorry for not responding in a timely manner。

I seem to see hope. But something is not right.

The key to the problem becomes how to read field names from JSON files (such as myParStn)


My target JSON is like this (for example: aa.json)

{
"myParStn": "222",
"myChaStn": "HH"
}

I followed your advice and made the following modifications:

var abc="111";
rwJSON(myparStn,abc);

function rwJSON(n,v) {
    // read the json file's contents
    var myJSON = readFile(pathToMyJSONFile);
    if (!myJSON)
        return alert('Could not read JSON file.');
    // parse as json
    myJSON = JSON.parse(myJSON);
    // input text;
    //myJSON.n = v;   
     myJSON[n] = v;  
    var f = writeFile(pathToMyJSONFile, JSON.stringify(myJSON, undefined, '\t'));
}

 The result indicates that myParStn is undefined,

 

So I guess, first we need to read myParStn from aa.json.
So I added in front of it:

var abc="111";
var myJSON = readFile(pathToMyJSONFile);
if (!myJSON)
return alert('Could not read JSON file.');
// parse as json
myJSON = JSON.parse(myJSON);
rwJSONw(myJSON.myParStn,abc);

The result of aajson is as follows:

{
"myParStn": "222",
"myChaStn": "HH",
"222": "111"
}

Obviously, I still haven't obtained the myParStn field itself.
I just got the original value of myParStn

 

 

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
Community Expert ,
Aug 08, 2025 Aug 08, 2025

Try the following

var abc="111";
rwJSON("myparStn",abc);

function rwJSON(n,v) {
    // read the json file's contents
    var myJSON = readFile(pathToMyJSONFile);
    if (!myJSON)
        return alert('Could not read JSON file.');
    // parse as json
    myJSON = JSON.parse(myJSON);
    // input text;
    //myJSON.n = v;   
     myJSON[n] = v;  
    var f = writeFile(pathToMyJSONFile, JSON.stringify(myJSON, undefined, '\t'));
}

-Manan

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 ,
Aug 08, 2025 Aug 08, 2025
LATEST

You are an invincible god.
Success!
Thank you very much.

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