How to do an inline ternary operation or if else in a JSON array?

Viewed 10

I have JSON array structure like below.

{
    "appName": "MyApp",
    "restricted_mode": true,

    "some_array": [
          { "module_1": "abc", "type": "internal", "company": "Google", "storage": "cloud" },
          { "module_2": "xyz", "type": "external", "company": "Amazon", "storage": "local" },
      ]
}

Above works great but is it possible to set type of module_2 to external only if restricted_mode is set to false? How can I do an inline ternary operation or if else here?

1 Answers

if(a.restricted_mode === false){
    a.some_array[1].type = "external"
}

you can write this code after creating your json

Related