Python json.py() Examples

The following are code examples for showing how to use json.py(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: handy-tools   Author: anuragbanerjee   File: csv-to-json.py    MIT License 5 votes vote down vote up
def showInfo():
  print "CSV to JSON Converter"
  print "USAGE: csv-to-json.py <CSVFILE>"
  print ""
  print "Use I/O redirection for creating resulting files. Example:"
  print "python ./csv-to-json.py sample.csv > sample.json" 
Example 2
Project: zoe   Author: DistributedSystemsGroup   File: request_handler.py    Apache License 2.0 5 votes vote down vote up
def dumps(obj, **kwargs):
    """Escape characters."""
    # https://github.com/mitsuhiko/flask/blob/master/flask/json.py
    return json.dumps(obj, **kwargs) \
        .replace(u'<', u'\\u003c') \
        .replace(u'>', u'\\u003e') \
        .replace(u'&', u'\\u0026') \
        .replace(u"'", u'\\u0027') 
Example 3
Project: zoe   Author: DistributedSystemsGroup   File: request_handler.py    Apache License 2.0 5 votes vote down vote up
def tojson_filter(obj, **kwargs):
    """Filter for JSON output in templates."""
    # https://github.com/mitsuhiko/flask/blob/master/flask/json.py
    return Markup(dumps(obj, **kwargs)) 
Example 4
Project: Django_Handsontable-sample   Author: thinkAmi-sandbox   File: views.py    The Unlicense 5 votes vote down vote up
def get(self, request, *args, **kwargs):
        # http://stackoverflow.com/questions/13527843/accessing-primary-key-from-url-in-django-view-class
        details = Detail.objects.filter(header__pk=self.kwargs.get('pk')).select_related().all()

        # serializerがjson.dumpしてるので、JsonResponseを使うと二重でjson.dump()してしまうため、普通のレスポンスを使う
        # http://stackoverflow.com/questions/26373992/use-jsonresponse-to-serialize-a-queryset-in-django-1-7
        # https://github.com/django/django/blob/1d1e246db6ae8a8c7b9a54f3485809a36c5ee373/django/core/serializers/json.py#L63
        # https://github.com/django/django/blob/190d2ff4a7a392adfe0b12552bd71871791d87aa/django/http/response.py#L526
        return HttpResponse(
            serializers.serialize('handsontablejson', details),
            content_type='application/json'
        ) 
Example 5
Project: heaviside   Author: jhuapl-boss   File: parser.py    Apache License 2.0 5 votes vote down vote up
def json_text_():
    """Returns the parser for JSON Text"""
    # Taken from https://github.com/vlasovskikh/funcparserlib/blob/master/funcparserlib/tests/json.py
    # and modified slightly
    unwrap = lambda x: x.value

    null = (n('null') | n('Null')) >> const(None) >> unwrap

    value = forward_decl()
    member = (string >> unwrap) + op_(u':') + value >> tuple
    object = (
        op_(u'{') +
        maybe(member + many(op_(u',') + member) + maybe(op_(','))) +
        op_(u'}')
        >> make_object)
    array = (
        op_(u'[') +
        maybe(value + many(op_(u',') + value) + maybe(op_(','))) +
        op_(u']')
        >> make_array)

    value.define(
        null
        | (true >> unwrap)
        | (false >> unwrap)
        | object
        | array
        | (number >> unwrap)
        | (string >> unwrap))

    return value