Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use if statement inside JSON?

How to use if statement inside JSON Here is the code: .......................................................................................

var config =
             [
                {
                    "name"      : "SiteTitle",
                    "bgcolor"   : "",
                    "color"     : "",
                    "position"  : "TL",
                    "text"      : "step1",
                    "time"      : 5000
                },
                {
                    "name"      : "Jawal",
                    "bgcolor"   : "",
                    "color"     : "",
                    "text"      : "step2",
                    "position"  : "BL",
                    "time"      : 5000
                },
                {
                    "name"      : "Password",
                    "bgcolor"   : "",
                    "color"     : "",
                    "text"      : "step3",
                    "position"  : "TL",
                    "time"      : 5000
                }
            ],

            //define if steps should change automatically
            autoplay    = false,
            //timeout for the step
            showtime,
            //current step of the tour
            step        = 0,
            //total number of steps
            total_steps = config.length;

This is the required result something like this:

    var config =
         [

    if(page==true)  {               
            {
                "name"      : "SiteTitle",
                "bgcolor"   : "",
                "color"     : "",
                "position"  : "TL",
                "text"      : "step1",
                "time"      : 5000
            },
            {
                "name"      : "Jawal",
                "bgcolor"   : "",
                "color"     : "",
                "text"      : "step2",
                "position"  : "BL",
                "time"      : 5000
            }
    } else {
            {
                "name"      : "Password",
                "bgcolor"   : "",
                "color"     : "",
                "text"      : "step3",
                "position"  : "TL",
                "time"      : 5000
            }
    }
        ],

            //define if steps should change automatically
            autoplay    = false,
            //timeout for the step
            showtime,
            //current step of the tour
            step        = 0,
            //total number of steps
            total_steps = config.length;

Actually this way is wrong and makes a JavaScript syntax error.

like image 310
Montaser El-sawy Avatar asked Feb 25 '13 07:02

Montaser El-sawy


People also ask

Can we put condition in JSON?

Use the JSON_EXISTS condition to test whether a specified JSON value exists in JSON data. This condition returns TRUE if the JSON value exists and FALSE if the JSON value does not exist. Use this clause to specify the JSON data to be evaluated. For expr , specify an expression that evaluates to a text literal.

How do you write if else in JSON?

var config = [ if(page==true) { { "name" : "SiteTitle", "bgcolor" : "", "color" : "", "position" : "TL", "text" : "step1", "time" : 5000 }, { "name" : "Jawal", "bgcolor" : "", "color" : "", "text" : "step2", "position" : "BL", "time" : 5000 } } else { { "name" : "Password", "bgcolor" : "", "color" : "", "text" : "step3 ...

How do you check if a JSON object contains a key or not?

JsonObject::containsKey() returns a bool that tells whether the key was found or not: true if the key is present in the object. false if the key is absent of the object.

Can you pass a function in JSON?

Yes, you can. There are tons of ways to do it. var tmpFunc = new Function(codeToRun); tmpFunc(); Whether it was JSON at any stage should be irrelevant.


2 Answers

Validating the JSON Schema Draft-07, JSON now supports the if...then...else keywords for conditional data representation.

Here is a quick example:

{
    "type": "integer",
    "minimum": 1,
    "maximum": 1000,
    "if": { "minimum": 100 },
    "then": { "multipleOf": 100 },
    "else": {
        "if": { "minimum": 10 },
        "then": { "multipleOf": 10 }
    }
}

Edits

Using the OP's example in the context, it can be written like this:

{
    "if": {
        "page": true
    },
    "then": [
        {
            "name": "SiteTitle",
            "bgcolor": "",
            "color": "",
            "position": "TL",
            "text": "step1",
            "time": 5000
        },
        {
            "name": "Jawal",
            "bgcolor": "",
            "color": "",
            "text": "step2",
            "position": "BL",
            "time": 5000
        }
    ],
    "else": [
        {
            "name": "Password",
            "bgcolor": "",
            "color": "",
            "text": "step3",
            "position": "TL",
            "time": 5000
        }
    ]
}
like image 62
Erisan Olasheni Avatar answered Oct 15 '22 09:10

Erisan Olasheni


That's regular JavaScript, not JSON. Move the if statement outside:

if (page) {
    var config = [
        {
            "name"      : "SiteTitle",
            "bgcolor"   : "",
            "color"     : "",
            "position"  : "TL",
            "text"      : "step1",
            "time"      : 5000
        }, {
            "name"      : "Jawal",
            "bgcolor"   : "",
            "color"     : "",
            "text"      : "step2",
            "position"  : "BL",
            "time"      : 5000
        }
    ];
} else {
    var config = [
        {
            "name"      : "Password",
            "bgcolor"   : "",
            "color"     : "",
            "text"      : "step3",
            "position"  : "TL",
            "time"      : 5000
        }
    ];
}
like image 45
Blender Avatar answered Oct 15 '22 08:10

Blender