Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing a property value in JSON

If I have a JSON structure that looks something like this:

var user = {
    map: {
        width: 785,
        height: 791
    },
    image: {
        name: "image.png",
        size: {width:32}
    },
    properties:[{
        firstName: "Bob",
        lastName: "Jones",
    }]
};

How would I change (after creation) the value of the firstName property to "Jane"?

I am fairly new to JSON, and I'm just trying to figure out how to make this one change for now. Any help would be greatly appreciated.

like image 646
simmB Avatar asked May 25 '26 13:05

simmB


1 Answers

Well, one reason for your confusion might be that this is not JSON at all. JSON is a text format used for serialising objects. This is just a literal object in Javascript.

To change the firstName property, you would access the first item in the properties array in the user object:

user.properties[0].firstName = "Jane";
like image 170
Guffa Avatar answered May 28 '26 02:05

Guffa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!