-1

i have a separate JSON array file which i have my data, but according to user inputs i need to change three values in my JSON then do my http request. i'm stuck at assigning the values i get from my users to the pre define JSON array file i have.

example.json

{
  "packageName": "example",
  "packageType": "example",
  "navigationType": "example",
  "description":"",
  "consoleAccessLimit": [
    {
      "accessType": "example4",
      "accessLimit": 2
    },
    {
      "accessType": "example3",
      "accessLimit": 1
    },
    {
      "accessType": "example2",
      "accessLimit": 1
    }
  ]}

i need to change accesslimit of example4, accesslimit of example 3 and accesslimit of example 1

my code following

function askme() {
    askDetails("Enter example1 Count:", /.+/, function(scount) {
        askDetails("Enter example 3 Count:", /.+/, function (acount) {
            askDetails("Enter example 2 Count:", /.+/,function (wcount) {
                askDetails("Enter example 4 count:",/.+/,function (price) {
                    var youexample = (require('./example/example.json'));
// how do i assign the values to my example.json 


                })

            })

        })

    });

}

please help folks thank you

frodo
  • 51
  • 2
  • 11

2 Answers2

0
var fs = require('fs');
fs.readFile('data.json',function(err,content){
if(err) throw err;
  var parseJson = JSON.parse(content);
  //modify json content here
  .writeFile('data.json',JSON.stringify(parseJson),function(err){
  if(err) throw err;
  })
})

You have to read and write file in order to modify your .json file

Asif Saeed
  • 1,985
  • 14
  • 24
0

Your code isn't structured very well. You're asking the user for input that will change the array values of the JSON. You should restructure your code in a way that allows the questions and the JSON array to be more flexible. For example, maybe today you have four values in the array but in a month that might need to change to six values.

for (var x=0; x < youexample.consoleAccessLimit.length; x++) {
    askDetails("Enter example"+x+" Count:", /.+/, function(count) {
        youexample.consoleAccessLimit[x].accessLimit = count;
    }
}
console.log("youexample result: ", youexample)

What I'm doing here is creating a for loop that runs through the length of the consoleAccessLimit array in the youexample JSON.

Each time through the loop, it runs the askDetails function which gets user input and passes the result into the function with the value count. Then we're assigning the value of count to the specific youexample.consoleAccessLimit index corresponding to x.

Since you have three items in the array, the loop will run three times and assign the result of the user input to each of them.

So if the user enters 5 for the first one, then 9, then 2, the result of the JSON will look like this in the end:

{
  "packageName": "example",
  "packageType": "example",
  "navigationType": "example",
  "description":"",
  "consoleAccessLimit": [
    {
      "accessType": "example4",
      "accessLimit": 5
    },
    {
      "accessType": "example3",
      "accessLimit": 9
    },
    {
      "accessType": "example2",
      "accessLimit": 2
    }
  ]}

Open your web developer console to see the result of the console.log output.

Dr. Cool
  • 3,713
  • 3
  • 22
  • 26