Popular recipes tagged "json"http://code.activestate.com/recipes/tags/json/2016-05-27T01:07:42-07:00ActiveState Code RecipesConvert JSON to PDF with Python and xtopdf (Python) 2014-12-10T18:02:14-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/578979-convert-json-to-pdf-with-python-and-xtopdf/ <p style="color: grey"> Python recipe 578979 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/pdfwriter/">pdfwriter</a>, <a href="/recipes/tags/sys/">sys</a>). </p> <p>This recipe show the basic steps needed to convert JSON input to PDF output, using Python and xtopdf, a PDF creation toolkit. xtopdf is itself written in Pytho, and uses the ReportLab toolkit internally.</p> <p>We set up some needed values, such as the output PDF file name, the font name and size, the header and footer, and the input lines for the body of the PDF output; all these values are passed in JSON format (in a single dictionary) to a function that uses those values to generate a PDF file with the desired content.</p> <p>The code is intentionally kept simple so as to require the least amount of code needed to demonstrate the techniques involved. But it can be generalized or extended to more complex situations.</p> JSON Formatted Logging (Python) 2016-05-27T01:07:42-07:00Michael Blan Palmerhttp://code.activestate.com/recipes/users/4194130/http://code.activestate.com/recipes/580667-json-formatted-logging/ <p style="color: grey"> Python recipe 580667 by <a href="/recipes/users/4194130/">Michael Blan Palmer</a> (<a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/logging/">logging</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>I have created a package that outputs JSON formatted lines to a log file. It can make use of the standard logging parameters and/or take custom input. The use of JSON in the log file allows for easy filtering and processing.</p> Configurable JSON Extensions for Python (Python) 2016-05-22T19:00:54-07:00Michael Blan Palmerhttp://code.activestate.com/recipes/users/4194130/http://code.activestate.com/recipes/580664-configurable-json-extensions-for-python/ <p style="color: grey"> Python recipe 580664 by <a href="/recipes/users/4194130/">Michael Blan Palmer</a> (<a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>The concept behind the package is to build a single class per type you want to add to json in Python. The new class will have a method for encoding to json and a method for decoding from json. The classes are then loaded into an encoder object and a decoder object that are hooked into the standard json loads and dumps functions.</p> Validate data easily with JSON Schema (Python) 2015-12-06T21:23:48-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579135-validate-data-easily-with-json-schema/ <p style="color: grey"> Python recipe 579135 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/data/">data</a>, <a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/jsonschema/">jsonschema</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>). </p> <p>This recipe shows how to use the jsonschema Python library, which implements the JSON Schema specification, to easily validate your Python data. It sends good output to stdout and bad output to stderr, for demo purposes.</p> <p>jsonschema Python library: <a href="https://pypi.python.org/pypi/jsonschema" rel="nofollow">https://pypi.python.org/pypi/jsonschema</a></p> <p>JSON Schema: <a href="http://json-schema.org/" rel="nofollow">http://json-schema.org/</a></p> dynamically changing encoder (for json) with metaclass (class factory) (Python) 2013-10-24T11:31:44-07:00-http://code.activestate.com/recipes/users/4188267/http://code.activestate.com/recipes/578696-dynamically-changing-encoder-for-json-with-metacla/ <p style="color: grey"> Python recipe 578696 by <a href="/recipes/users/4188267/">-</a> (<a href="/recipes/tags/class_factory/">class_factory</a>, <a href="/recipes/tags/encode/">encode</a>, <a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/metaclass/">metaclass</a>, <a href="/recipes/tags/metaclasses/">metaclasses</a>). </p> <p>The <em>json.dumps</em> need to be feed with some class (cls =someClass) but what if we want to change the class dynamically? This example can be done by declaring the <em>listOfClasses</em> in class level of course, but the idea is to be changeable. This can be done by the class factory function <em>encoderFacory</em></p> easyjson.py - parsing JSON from buffer and from file (Python) 2013-05-27T05:32:07-07:00Thomas Lehmannhttp://code.activestate.com/recipes/users/4174477/http://code.activestate.com/recipes/578529-easyjsonpy-parsing-json-from-buffer-and-from-file/ <p style="color: grey"> Python recipe 578529 by <a href="/recipes/users/4174477/">Thomas Lehmann</a> (<a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/parser/">parser</a>). Revision 4. </p> <p><strong>JSON Parser</strong>:</p> <ul> <li>Refering to <a href="http://www.json.org/" rel="nofollow">http://www.json.org/</a></li> <li>Just one simple Python file you can integrate where you want to</li> <li>No imports (very important) -> no dependencies!!!</li> <li>Should work with really older versions of Python!!!</li> </ul> <p><strong>Todo's</strong>:</p> <ul> <li>Doesn't cover full number format</li> <li>...</li> </ul> <p><strong>Done</strong></p> <ul> <li>Allows string in string (revision 2)</li> <li>Covers objects in an array (revision 2)</li> <li>Provides a mechanism to allow other dictionaries (like collections.OrderedDict) (revision 3)</li> <li>Conversion of numbers to integer or float types (revision 4)</li> </ul> JSON instead of pickle for memcached (Python) 2012-01-10T22:31:36-08:00pavelhttp://code.activestate.com/recipes/users/4171837/http://code.activestate.com/recipes/578011-json-instead-of-pickle-for-memcached/ <p style="color: grey"> Python recipe 578011 by <a href="/recipes/users/4171837/">pavel</a> (<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/memcache/">memcache</a>, <a href="/recipes/tags/memcached/">memcached</a>, <a href="/recipes/tags/pickle/">pickle</a>). </p> <p>Standard memcache client uses pickle as a serialization format. It can be handy to use json, especially when another component (e.g. backend) does'n know pickle, but json yes.</p> JSON pretty printer (Python) 2011-06-28T11:23:13-07:00Noufal Ibrahimhttp://code.activestate.com/recipes/users/4173873/http://code.activestate.com/recipes/577772-json-pretty-printer/ <p style="color: grey"> Python recipe 577772 by <a href="/recipes/users/4173873/">Noufal Ibrahim</a> (<a href="/recipes/tags/json/">json</a>). </p> <p>Simple script to be used as a shell pipeline to pretty print JSON output. Quite useful when using couchdb with curl.</p> <p>This is a shell function which uses Python to do all ork.</p> nicerest: pretty-print JSON output (JavaScript) 2011-01-18T22:59:12-08:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577549-nicerest-pretty-print-json-output/ <p style="color: grey"> JavaScript recipe 577549 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/curl/">curl</a>, <a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/node/">node</a>, <a href="/recipes/tags/nodejs/">nodejs</a>, <a href="/recipes/tags/rest/">rest</a>). Revision 3. </p> <p>Save this to "nicerest", <code>chmod +x</code>-it, and pipe your REST API <code>curl</code> calls through this for nicer output. It will notice HTTP headers (curl's <code>-i</code> option) and skips those before attempting to pretty-print the following JSON.</p> <p>Note: This is currently using node 0.2. I should update for 0.3 changes (I think <code>process.openStdin</code> changed).</p> Convert XML into JSON (Python dicts and lists structure) (Python) 2011-08-22T13:30:33-07:00Nikolahttp://code.activestate.com/recipes/users/4176190/http://code.activestate.com/recipes/577494-convert-xml-into-json-python-dicts-and-lists-struc/ <p style="color: grey"> Python recipe 577494 by <a href="/recipes/users/4176190/">Nikola</a> (<a href="/recipes/tags/converter/">converter</a>, <a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/xml/">xml</a>). Revision 2. </p> <p>There are many ways to convert a specific XML dialect into JSON for easier consumption by Javascript code. This recipe works for some XML-based data, where the format is not really a document and html tags are used as field placeholders. You can use it as-is, but it is intended to serve as a starting point where you can plug your specific needs.</p> PyDON - An alternative to XML (Python) 2010-12-24T08:28:15-08:00Lost Protocolhttp://code.activestate.com/recipes/users/4176279/http://code.activestate.com/recipes/577508-pydon-an-alternative-to-xml/ <p style="color: grey"> Python recipe 577508 by <a href="/recipes/users/4176279/">Lost Protocol</a> (<a href="/recipes/tags/configuration/">configuration</a>, <a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/xml/">xml</a>, <a href="/recipes/tags/xmlrpc/">xmlrpc</a>). Revision 2. </p> <p>Many use XML to store configuration data, some use INI files others use Windows Registries and some like JSON. But given the fact that Python is such a powerful language, its easy to create/use such representations. PyDON stands for Python Dictionary Object Notation. It LOOKS similar to JSON but doesn't require you to create any parsers. Plus it can contain Python code which is even better :-).</p> <p>More on this here &gt; <a href="http://lostp.99k.org/files/PyDON.pdf" rel="nofollow">http://lostp.99k.org/files/PyDON.pdf</a></p> transform command line arguments to args and kwargs for a method call (Python) 2010-03-21T22:41:42-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577122-transform-command-line-arguments-to-args-and-kwarg/ <p style="color: grey"> Python recipe 577122 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/argv/">argv</a>, <a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/json/">json</a>). Revision 2. </p> <p>For many of my Python modules I find it convenient to provide a small <code>if __name__ == "__main__": ...</code> block to be able to call individual methods from the command line. This requires some kind of translation of command-line string arguments to <code>args</code> and <code>kwargs</code> for the method call. This recipe uses a few conventions to do that:</p> <ul> <li>the first argument is the method name</li> <li>positional args are positional (duh)</li> <li>"key=value" is a keyword argument</li> <li>an attempt is made to interpret arguments as JSON to allow specifying types other than string</li> </ul> Javascript - JSON Parser (only for study) (JavaScript) 2010-07-27T19:36:50-07:00Danillo Souzahttp://code.activestate.com/recipes/users/4174445/http://code.activestate.com/recipes/577337-javascript-json-parser-only-for-study/ <p style="color: grey"> JavaScript recipe 577337 by <a href="/recipes/users/4174445/">Danillo Souza</a> (<a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/parse/">parse</a>). </p> <p>JSON parser for any object in the script.</p> <p>OBS: Need a way to identify methods.</p> ExecSql.cgi returning JSON for SQL (Python) 2009-11-28T02:00:30-08:00Martchenkohttp://code.activestate.com/recipes/users/4172446/http://code.activestate.com/recipes/576971-execsqlcgi-returning-json-for-sql/ <p style="color: grey"> Python recipe 576971 by <a href="/recipes/users/4172446/">Martchenko</a> (<a href="/recipes/tags/cgi/">cgi</a>, <a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/sql/">sql</a>). </p> <p>CGI script getting JSON for SQL request</p> Convert XML to Dictionary (Python) 2009-10-28T10:42:17-07:00Adam M Prosthttp://code.activestate.com/recipes/users/4172124/http://code.activestate.com/recipes/576940-convert-xml-to-dictionary/ <p style="color: grey"> Python recipe 576940 by <a href="/recipes/users/4172124/">Adam M Prost</a> (<a href="/recipes/tags/convert/">convert</a>, <a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/simple/">simple</a>, <a href="/recipes/tags/xml/">xml</a>). Revision 2. </p> <p>A simple function to convert a headerless XML string into a dictionary using only simplejson and re.</p>