HiveBrain v1.2.0
Get Started
← Back to all entries
snippetpythonMinor

Determine if a file is either in JSON or XML format

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
formatfilexmleitherdeterminejson

Problem

The purpose of this function is to determine if report_input_file is either in JSON format or XML format, this is what I came up with, just want to know if this is best practice / pythonic way of achieving this?

def parse_report_file(self, report_input_file):
    """ Checks if input report file is of type json or xml """
    parsed_report_file = None
    try:
        parsed_report_file = self._parse_json_file(report_input_file)
        self.is_json_report_file = True
    except Exception, e:
        parsed_report_file = self._parse_xml_file(report_input_file)
        self.is_json_report_file = False

    return parsed_report_file

Solution

Well, if you only need to distinguish between JSON and XML formats I'd keep it simple. If the encoding of that string is known to you (or is ASCII or UTF), then looking at the very first char in the file should be enough. Your code looks pythonic to me, but I'd rather do it this way:

def parse_report_file(report_input_file):
    with open(report_input_file) as unknown_file:
        c = unknown_file.read(1)
        if c != '<':
            return 'Is JSON'
        return 'Is XML'


While it is legal for JSON data structures to begin with null, true, false you can avoid those situations if you already know a bit about your data structures.

If you also need to distinguish between JSON, XML and other random file, you should simply strip any whitespace before you look at the "first" char.

Code Snippets

def parse_report_file(report_input_file):
    with open(report_input_file) as unknown_file:
        c = unknown_file.read(1)
        if c != '<':
            return 'Is JSON'
        return 'Is XML'

Context

StackExchange Code Review Q#137920, answer score: 7

Revisions (0)

No revisions yet.