Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable in JSON

I don't have much experience with JSON, I want to know if something like this is possible.

{
    "variable": "A really long value that will take up a lot of space if repeated",

    "array": [variable, variable, variable]
}

Obviously that isn't valid, but I want to know if there is a way to do this. I tried using "variable" but of course that just sets the array item to the string "variable". The reason I want to do this is I need to repeat long values in a multidimensional array, which takes up a lot of space.

Thanks.

like image 389
Suffick Avatar asked Jan 31 '26 00:01

Suffick


1 Answers

If you are willing to do some post-processing on the JSON after parsing it, then you can use a token value in your array, and replace the token after parsing with the variable. Example:

{
    "variable": "A really long value",
    "array": ["variable", "variable", "variable"]
}

Then, in your code that parses:

var obj = JSON.parse(str);
for (var i=0; i<obj.array.length; i++)
{
    obj.array[i] = obj[obj.array[i]];
}
like image 181
Zach Avatar answered Feb 01 '26 15:02

Zach