Answers.Body: <p>I'd strongly recommend looking at the <a href="https://github.com/pixelandtonic/ElementAPI" rel="nofollow">Element API</a> plugin for this.</p>
  
  <p>If you're dead set against that, there are <a href="https://craftcms.stackexchange.com/questions/20/how-can-i-retrieve-channel-data-in-json-format">other examples</a> of how to pull out section data in Craft for rolling your own JSON.</p>
  
  <p>For Matrix it'd be no different that if your were pulling Matrix data out to be output in HTML instead of JSON (looping through blocks, grabbing fields, etc). Lots of examples in the <a href="https://craftcms.com/docs/matrix-fields#templating" rel="nofollow">Matrix templating</a> docs.</p>
  
  <p>The error you're getting is because when you simply output <code>entry.myMatrixHandle</code>, you're technically given back an <a href="https://craftcms.com/docs/templating/elementcriteriamodel" rel="nofollow">ElementCriteriaModel</a> object and the Element API plugin tries to convert that to a string to display in the JSON data and it doesn't know how to convert that object to a string.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-10T03:28:27.660Z
Answers.LastActivityDate: 2016-03-10T03:28:27.660Z
Answers.UserDisplayName: Brad Bell
Body: <p>Have a JSON object that I'm trying to pass to my Javascript through a script tag in my markup like so:</p>
  
  <pre><code>&lt;script type="text/json" id="json-data"&gt;
  {
    "terms": "{{ entry.termsAgreement }}",
    "sectionHeader": {{ entry.formSection }}
  }
  </code></pre>
  
  <p></p>
  
  <p>However, I'm getting an error that says: </p>
  
  <p><code>Object of class Craft\ElementCriteriaModel could not be converted to string</code>.</p>
  
  <p>formSection is a Matrix field. How can I pass this along to have access to it in Javascript? I tried </p>
  
  <p><code>JSON.stringify({{ entry.formSection }})</code> </p>
  
  <p>but no luck.</p>
  
  <p>Thanks.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-03-09T19:56:58.833Z
Comments.Text: If the JSON can come from an external request, you might consider using the [Element API from P&T](https://github.com/pixelandtonic/ElementAPI). Either way, you're probably going to want to be more explicit about looping through and outputting _what you need_ from the Matrix fields. You may be able to easily convert the model to JSON somehow, but I expect you'll end up with tons of extra information that'll just add cruft to your page.
Comments.UserDisplayName: Matt Stein
Tags: <matrix><json>
Title: How to pass an entry's Matrix fields as JSON?
ThreadText: # How to pass an entry's Matrix fields as JSON?
  
  (Asked by: username_0 on 2016-03-09)
  
  Have a JSON object that I'm trying to pass to my Javascript through a script tag in my markup like so:
  
  ```
  <script type="text/json" id="json-data">
  {
    "terms": "{{ entry.termsAgreement }}",
    "sectionHeader": {{ entry.formSection }}
  }
  
  ```
  
  However, I'm getting an error that says:
  
  `Object of class Craft\ElementCriteriaModel could not be converted to string`.
  
  formSection is a Matrix field. How can I pass this along to have access to it in Javascript? I tried
  
  `JSON.stringify({{ entry.formSection }})`
  
  but no luck.
  
  Thanks.
  
  **Tags:** matrix, json
  
  > **Comments:**
  > **username_1**: If the JSON can come from an external request, you might consider using the [Element API from P&T](https://github.com/pixelandtonic/ElementAPI). Either way, you're probably going to want to be more explicit about looping through and outputting _what you need_ from the Matrix fields. You may be able to easily convert the model to JSON somehow, but I expect you'll end up with tons of extra information that'll just add cruft to your page.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2016-03-10. Score: 1 | Accepted answer
  
  I'd strongly recommend looking at the [Element API](https://github.com/pixelandtonic/ElementAPI) plugin for this.
  
  If you're dead set against that, there are [other examples](https://craftcms.stackexchange.com/questions/20/how-can-i-retrieve-channel-data-in-json-format) of how to pull out section data in Craft for rolling your own JSON.
  
  For Matrix it'd be no different that if your were pulling Matrix data out to be output in HTML instead of JSON (looping through blocks, grabbing fields, etc). Lots of examples in the [Matrix templating](https://craftcms.com/docs/matrix-fields#templating) docs.
  
  The error you're getting is because when you simply output `entry.myMatrixHandle`, you're technically given back an [ElementCriteriaModel](https://craftcms.com/docs/templating/elementcriteriamodel) object and the Element API plugin tries to convert that to a string to display in the JSON data and it doesn't know how to convert that object to a string.
  
  
  ---
  
  