0

I have some data in my browser's network HTTP response that i would like to use to create JS variables. Is this possible?

{
 "stepId" : "step1",
"formInfo" : {
"schema" : {
  "title" : "intro"
    }
  }
},

I have these information in my http response, how can i create a JS variable for "stepId"?

Thanks

John P.
  • 35
  • 7
  • 1
    If the response is in JSON format, you need to use `JSON.parse()` to convert it to a JS object/array. Then you can use ordinary object or array syntax to get parts of it. – Barmar Jan 03 '18 at 19:57
  • I also think that if you're using jQuery to do this that it will automatically parse and return a JSON object in your `complete/then()` function. – Jhecht Jan 03 '18 at 20:03
  • @Barmar, yes the response is in JSON, do you have an example of how to do this? – John P. Jan 03 '18 at 20:37

1 Answers1

0

If you get that specific JSON object as response from, for example an ajax get method (HTTP GET), you may get your variable stepId by the following

$.get("demo_test.php", function(data, status){
    var stepId = JSON.parse(data)["stepId"];
});
João Pimentel Ferreira
  • 14,289
  • 10
  • 80
  • 109