title: How do I use an Avro schema to validate JSON?
body: <p>I'd like to know the feasibility of using an Avro schema to validate JSON that comes into my app.  In <a href="http://apache-avro.679487.n3.nabble.com/Validating-a-JSON-schema-td2731411.html" rel="nofollow">this post</a>, Doug Cutting suggests using the <code>jsontofrag</code> tool that comes with the avro-tools jar.  His example is a trivial one of a JSON "document" that is just a number:</p>
  
  <pre><code>echo 2 | java -jar avro-tools.jar jsontofrag '"int"' - | java -jar avro-tools.jar fragtojson '"int"' - 
  </code></pre>
  
  <p>While this works, I'd like to know how to do it with a more interesting JSON doc.</p>
  
  <p>When I try this with the example JSON doc and schema on the Avro website it fails, like so:</p>
  
  <p><strong>The Avro schema:</strong></p>
  
  <pre><code>{"namespace": "example.avro",
   "type": "record",
   "name": "User",
   "fields": [
       {"name": "name", "type": "string"},
       {"name": "favorite_number",  "type": ["int", "null"]},
       {"name": "favorite_color", "type": ["string", "null"]}
   ]
  }
  </code></pre>
  
  <p><strong>Example JSON doc</strong></p>
  
  <pre><code>{"name": "Ben",
   "favorite_number": 7,
   "favorite_color": "red"}
  </code></pre>
  
  <p>But when I try to do it with:</p>
  
  <pre><code>cat user.json | java -jar avro-tools.jar jsontofrag user.avsc - | java -jar avro-tools.jar fragtojson user.avsc -
  </code></pre>
  
  <p>It get this error (stack trace elided):</p>
  
  <pre><code>Exception in thread "main" org.apache.avro.SchemaParseException: org.codehaus.jackson.JsonParseException: 
  Unexpected character ('u' (code 117)): 
  expected a valid value (number, String, array, object, 'true', 'false' or 'null') 
  at [Source: java.io.StringReader@74dca977; line: 1, column: 2]
  </code></pre>
  
  <p>Any ideas on how to make this work?  Or another way to use an Avro schema to validate JSON?</p>