title: Conditional accessing Flask parameter from Javascript ignored
body: <p>I'm building a Flask app in which I sometimes, depending on certain conditions, need to pass certain data through render template to javascript.</p>
  <p>In python, I assemble the data object with either two or three properties:</p>
  <p>Scenario #1:</p>
  <pre><code>data['chart_data'] = tempData.to_dict(orient='records')
  data['FA_file'] = fa_file.to_dict(orient='records')
  </code></pre>
  <p>Scenario #2: add in the agency_metadata</p>
  <pre><code>data['chart_data'] = tempData.to_dict(orient='records')
  data['FA_file'] = fa_file.to_dict(orient='records')
  data['agency_metadata'] = agency_metadata.to_dict(orient='records')
  </code></pre>
  <p>I then pass this into render_template:</p>
  <pre><code>render_template('dashboard.html', data=data)
  </code></pre>
  <p>In my script, I need to check if the &quot;agency_metadata&quot; property exists. If it does, I pass it into a function, if it doesn't I leave it out. This is how I'm attempting to do that:</p>
  <pre><code>        let allData = {{data|tojson}};
          let graphData = {{data.chart_data | safe}}
          let fa_file = {{data.FA_file | safe}}
          let agency_metadata = &quot;NA&quot;
          if(allData.hasOwnProperty('agency_metadata')){
              agency_metadata = {{data.agency_metadata|tojson}}
          }
          $(document).ready(function (){   
              if (typeof graphData !== 'undefined') {
                  updateCharts(graphData, fa_file, agency_metadata)
              }
          })
  </code></pre>
  <p>The problem I'm running into is that if we're in scenario #1 where the agency_metadata isn't passed in the data variable, the browser still tries to evaluate <code>agency_metadata = {{data.agency_metadata|tojson}}</code> and then breaks (&quot;TypeError: Object of type Undefined is not JSON serializable&quot;). I've tried other ways of arranging the conditional, with other variables and nothing has worked. Is there an order of operations that I'm not understanding that causes this line of code to be read first before the if()?</p>