1

I’ve been trying for quite some time to get a JSON file and edit one of the values in the array (to be passed to PHP)

My current code is

j1 = $.getJSON("data.json") 
j1.responseJSON[name].job = "Carpenter"

When I use console.log(j1) after that, it logs the JSON the same as it was before (with name’s job being “Baker”). What am I doing wrong, and how can I fix it?

UPDATE 1: ADDED JSON

{
     name:{
          {"job":"Baker", "age":"twenty-three","educated":"yes"}
     }
}
1
  • 1
    i think we need to see the json. Commented Jul 14, 2018 at 0:49

1 Answer 1

4

$.getJSON("data.json") is async. You need a callback. Otherwise you set the value before the request is being done.

http://api.jquery.com/jQuery.getJSON/

$.getJSON("data.json", function(j1){
    j1.responseJSON[name].job = "Carpenter"
    console.log(j1)
})
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.