-3

I'm trying to incorporate the below function to populate the value of my JSON data field 'groupName'. - Anyway to incorporate this concept with a JSON file/field?

 "Groups" : { 
          "groupName" : function makeid() {
            var text = "";
            var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

            for (var i = 0; i < 5; i++)
            text += possible.charAt(Math.floor(Math.random() * possible.length));

        return text;
    }
},
Dr Upvote
  • 8,023
  • 24
  • 91
  • 204
  • 2
    Json can't contain functions, it's not a valid data type. You might be confusing it with a JS object as it's pretty much the same markup. – Adrian Aug 21 '18 at 15:14
  • Try getting rid of the "makeId" name for the function. Just "groupName": function(){ ... } Does that help? – daddygames Aug 21 '18 at 15:17
  • @daddygames, I'm curious how you think that would help. In the first part, JSON cannot contain functions. In the second, if this were a JavaScript object, the function name would be perfectly valid. – Fissure King Aug 21 '18 at 15:18
  • @FissureKing whether or not it works, it doesn't make sense to give the function a name in this context. JS Object or JSON isn't my concern. I'm not arguing against those statements. – daddygames Aug 21 '18 at 15:22
  • @daddygames, I would reconsider that position. Debugging can be greatly simplified by naming functions. – Fissure King Aug 21 '18 at 15:23
  • But if there is no reference to the function, then I don't see how it helps debugging. PM me if you feel the need to explain your position on that. I don't think this contributes the answer of this question. – daddygames Aug 21 '18 at 15:26

2 Answers2

1

You can't put functions directly in your json. You'll need to create a JS object to match your JSON structure and then simply stringify it to produce the JSON you need. Have a look at example below:

function makeid() {
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    for (var i = 0; i < 5; i++)
        text += possible.charAt(Math.floor(Math.random() * possible.length));

    return text;
}

var result = {
    Groups: {
        groupName: makeid()
    }
};


console.log(JSON.stringify(result));
Zawiszor
  • 528
  • 4
  • 10
0

As I said in the comments, you can't have a function within JSON as it's not a valid data type.

If you're confusing it with a JS object because of it's similarity in syntax, please consider the following:

    var test = { "Groups" : { 
              "groupName" : (function() {
                var text = "";
                var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    
                for (var i = 0; i < 5; i++)
                text += possible.charAt(Math.floor(Math.random() * possible.length));
    
            return text;
        })()
    }}
    
    console.log(test);

At this point you can stringify the object to give you JSON result.

Adrian
  • 8,271
  • 2
  • 26
  • 43