Python json.py() Examples

The following are code examples for showing how to use json.py(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don't like. You can also save this page to your account.

Example 1
Project: abe-bootstrap   Author: TryCoin-Team   File: readconf.py    (GNU Affero General Public License v3.0) View Source Project 6 votes vote down vote up
def add(conf, var, val):
    if var not in conf:
        conf[var] = val
        return

    if isinstance(val, dict) and isinstance(conf[var], dict):
        conf[var].update(val)
        return

    if not isinstance(conf[var], list):
        conf[var] = [conf[var]]
    if isinstance(val, list):
        conf[var] += val
    else:
        conf[var].append(val)

# Scan to end of JSON object.  Grrr, why can't json.py do this without
# reading all of fp? 
Example 2
Project: abe-bootstrap   Author: TryCoin-Team   File: readconf.py    (GNU Affero General Public License v3.0) View Source Project 6 votes vote down vote up
def add(conf, var, val):
    if var not in conf:
        conf[var] = val
        return

    if isinstance(val, dict) and isinstance(conf[var], dict):
        conf[var].update(val)
        return

    if not isinstance(conf[var], list):
        conf[var] = [conf[var]]
    if isinstance(val, list):
        conf[var] += val
    else:
        conf[var].append(val)

# Scan to end of JSON object.  Grrr, why can't json.py do this without
# reading all of fp? 
Example 3
Project: nanodesign   Author: Autodesk   File: compare-viewer-json.py    (license) View Source Project 6 votes vote down vote up
def main():
    global logger
    logger = logging.getLogger('compare-viewer-json')

    if (len(sys.argv) != 3):
        sys.stderr.write("**** ERROR: Wrong number of arguments.\n") 
        sys.stderr.write("Usage: compare-viewer-json.py <file1> <file2> \n")
        sys.exit(1)

    # Read the 1st file and create a DNA structure object from it.
    file_name1 = sys.argv[1]
    dna_structure1 = DnaStructure(file_name1)
    dna_structure1.read_json(file_name1)
    logger.info("")

    # Read the 2nd file and create a DNA structure object from it.
    file_name2 = sys.argv[2]
    dna_structure2 = DnaStructure(file_name2)
    dna_structure2.read_json(file_name2)

    # Compare the data.
    if not dna_structure1.compare(dna_structure2):
        sys.exit(1) 
Example 4
Project: heaviside   Author: jhuapl-boss   File: parser.py    (license) View Source Project 5 votes vote down vote up
def json_text():
    """Returns the parser for Json formatted data"""
    # 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))
    json_text = object | array

    return json_text 
Example 5
Project: handy-tools   Author: anuragbanerjee   File: csv-to-json.py    (license) View Source Project 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 6
Project: python-flask-security   Author: weinbergdavid   File: colors.py    (license) View Source Project 4 votes vote down vote up
def get_lexer(mime, explicit_json=False, body=''):

    # Build candidate mime type and lexer names.
    mime_types, lexer_names = [mime], []
    type_, subtype = mime.split('/', 1)
    if '+' not in subtype:
        lexer_names.append(subtype)
    else:
        subtype_name, subtype_suffix = subtype.split('+', 1)
        lexer_names.extend([subtype_name, subtype_suffix])
        mime_types.extend([
            '%s/%s' % (type_, subtype_name),
            '%s/%s' % (type_, subtype_suffix)
        ])

    # As a last resort, if no lexer feels responsible, and
    # the subtype contains 'json', take the JSON lexer
    if 'json' in subtype:
        lexer_names.append('json')

    # Try to resolve the right lexer.
    lexer = None
    for mime_type in mime_types:
        try:
            lexer = pygments.lexers.get_lexer_for_mimetype(mime_type)
            break
        except ClassNotFound:
            pass
    else:
        for name in lexer_names:
            try:
                lexer = pygments.lexers.get_lexer_by_name(name)
            except ClassNotFound:
                pass

    if explicit_json and body and (not lexer or isinstance(lexer, TextLexer)):
        # JSON response with an incorrect Content-Type?
        try:
            json.loads(body)  # FIXME: the body also gets parsed in json.py
        except ValueError:
            pass  # Nope
        else:
            lexer = pygments.lexers.get_lexer_by_name('json')

    return lexer