Debugger
Debugger

Reputation: 491

How to modify JSON array?

How can I update a value at a specific index in the following multi dimensional JSON array?

I would like to update the value of background-image placed inside the footer_logo node.

{
    "Machine1": {
        "sidebar_inner": {
            "img": "img/pill.png",
            "background-color": "#ffffff",
            "side_logo": {
                "background-image": "../footer_logo.png"
            }
        },
        "lb_footer": {
            "img": "img/bin.png",
            "footer_logo": {
                "background-image": "..img/footer_logo.png"
            }
        },
        "machine_stand": {
            "img": "img/machine_stand.png"
        },
        "side": {
            "backgroundcolor": "#ccc"
        }
    }
}

Upvotes: 1

Views: 106

Answers (4)

NatureShade
NatureShade

Reputation: 2237

arr["Machine1"]["lb_footer"]["footer_logo"]["background-image"] = mynewvalue;

Upvotes: 0

Adam
Adam

Reputation: 2077

To update nested values, you would chain keys (indexes are for arrays) together to find the appropriate value.

Here's the code, formatted for easy consumption:

var obj = {"Machine1": {
    "sidebar_inner": {
        "img": "img/pill.png",
        "background-color": "#ffffff",
        "side_logo": {
            "background-image": "../footer_logo.png"
        }
    },
    "lb_footer": {
        "img": "img/bin.png",
        "footer_logo": {
            "background-image": "..img/footer_logo.png"
        }
    },
    "machine_stand": { 
        "img": "img/machine_stand.png"
    }, 
    "side": {
        "backgroundcolor":"#ccc"
    }
}}

Examples

To update "footer_logo"'s background image: obj.Machine1.lb_footer,footer_logo.background-image = "something new";

To update "machine_stand's" img:

obj.machine_stand.img = "new/link.jpg";
// or use 'bracket notation', it's the same thing
obj['machine_stand']['img'] = "new/link.jpg"

Upvotes: 2

vaso123
vaso123

Reputation: 12391

Try this. A working jsfiddle

    var json= '{"Machine1":{"sidebar_inner":{"img":"img\/pill.png","background-color":"#ffffff","side_logo":{"background-image":"..\/footer_logo.png"}},"lb_footer":{"img":"img\/bin.png","footer_logo":{"background-image":"..img\/footer_logo.png"}},"machine_stand":{"img":"img\/machine_stand.png"},"side":{"backgroundcolor":"#ccc"}}}';
    var jsonObj = JSON.parse(json);
    jsonObj.Machine1.lb_footer.footer_logo['background-image'] = 'abc';

Upvotes: 1

Stefan Koenen
Stefan Koenen

Reputation: 2337

I've added a jsfiddle example here. It's prety simple, take a look at the code below.

var myJson = {
        "Machine1": {
            "sidebar_inner": {
                "img": "img/pill.png",
                "background-color": "#ffffff",
                "side_logo": {
                    "background-image": "../footer_logo.png"
                }
            },
            "lb_footer": {
                "img": "img/bin.png",
                "footer_logo": {
                    "background-image": "..img/footer_logo.png"
                }
            },
            "machine_stand": {
                "img": "img/machine_stand.png"
            },
            "side": {
                "backgroundcolor": "#ccc"
            }
        }
    };

    myJson.Machine1.lb_footer.footer_logo['background-image'] = 'New value.';

    alert(myJson.Machine1.lb_footer.footer_logo['background-image']);

Upvotes: 3

Related Questions