# Update plot with JSON data from POST request

**URL:** https://community.plotly.com/t/update-plot-with-json-data-from-post-request/12136
**Category:** plotly.js
**Created:** [July 27, 2018, 4:09am UTC](https://community.plotly.com/t/update-plot-with-json-data-from-post-request/12136 "2018-07-27T04:09:01Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![kevinmcl](https://avatars.discourse-cdn.com/v4/letter/k/8dc957/32.png) [@kevinmcl](https://community.plotly.com/u/kevinmcl)
#### Post date: [July 27, 2018, 4:09am UTC](https://community.plotly.com/t/update-plot-with-json-data-from-post-request/12136/1 "2018-07-27T04:09:01Z")

</div>

I’m writing an application using flask and AJAX. I am having trouble updating my plot with new data from a POST request. The JSON data returned from my POST request looks good, but I get the following errors when I try to plot it:

```
Uncaught TypeError: Cannot read property 'style' of undefined
Uncaught TypeError: Cannot read property 'xy' of undefined

```

When I first render my page the graph works fine.

run.py, main():

```
...
graphJSON = json.dumps(graph, cls=plotly.utils.PlotlyJSONEncoder)
return render_template('home.html', graphJSON=graphJSON)
....

```

home.html:

```
...
var graph = {{graphJSON | safe}};
console.log(graph)
Plotly.newPlot("graph-0", graph.data, graph.layout);
...

```

However, when I try to update the plot using data recived from a POST request I get errors.

run.py, updatePlot():

```
...
graphJSON = json.dumps(graph, cls=plotly.utils.PlotlyJSONEncoder)
return graphJSON
...

```

home.html:

```
...
  $.post('/updatePlot',
    function(response) {
      console.log(response)
      Plotly.newPlot("graph-0", response.data, response.layout);
    })
  }
...

```

I also noticed that the console displays the JSON data from the first request as a nested tree diagram, but the data from the POST request as raw text.

Could someone point me to an example? I can’t seem to find what I am looking for. Thank you!

---

<div class="post-metadata">

### Author: ![kevinmcl](https://avatars.discourse-cdn.com/v4/letter/k/8dc957/32.png) [@kevinmcl](https://community.plotly.com/u/kevinmcl)
#### Post date: [July 31, 2018, 7:12am UTC](https://community.plotly.com/t/update-plot-with-json-data-from-post-request/12136/2 "2018-07-31T07:12:55Z")

</div>

Found the solution: the JSON data needs to be parsed before it can be plotted.

> [@Pass JSON string directly to Plotly](https://community.plotly.com/t/pass-json-string-directly-to-plotly/1925):
>
> I’m trying to specify both the data and layout of my plot with JSON. Is this possible? From googling, I found an example with Plotly.d3.json, which almost does what I want, but it downloads the JSON from a URL – I can’t seem to pass a JSON string directly. I couldn’t find any documentation on it though.

```
...
$.post('/updatePlot',
    function(response) {
    var figure = JSON.parse(response); //Parse data before plotting
    console.log(figure)
    Plotly.newPlot("graph-0", figure.data, figure.layout);
    })
  }
...

```
