1

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"
        }
    }
}
4
  • 3
    Try to describe your question clearly, right now it is unclear and vague. Commented Dec 24, 2014 at 12:35
  • 2
    obj[findex][sindex][tindex] = myvalue Commented Dec 24, 2014 at 12:38
  • 1
    Being pedantic: This isn't a JSON array, it's a JS object, similar to a hash map in other languages. Commented Dec 24, 2014 at 12:40
  • @AyazShakoor, please see my answer for the background-image footer_logo. Commented Dec 24, 2014 at 12:47

4 Answers 4

3

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']);
Sign up to request clarification or add additional context in comments.

Comments

2

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"

Comments

1

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';

4 Comments

Why put the data first in a string and then parse it?
Do not understand. First, for demonstration, second, if you do not parse it, you can not access it as an object. Or do you meant, why not inject it directly in the parse the string?
I don't know what you mean by directly in the parse the string. But you could omit the quotes and then you don't need to parse. Sew the other answers.
Oh, I see. I do it, because I was not sure, is this coming from an AJAX, or is this in javascript. I've just want to demonstrate, what if it is a string.
0
arr["Machine1"]["lb_footer"]["footer_logo"]["background-image"] = mynewvalue;

1 Comment

lb_footer is twice, and no footer_logo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.