I have a Python object method that uses the json
module to write a collection of ordered dictionary objects as JSON strings to a file with UTF-8 encoding. Here is the code:
def write_small_groups_JSON_file( self, file_dir, file_name ):
with io.open( file_dir + file_name, 'w', encoding='utf-8' ) as file:
JSON_obj = ''
i = 0
for ID in self.small_groups.keys():
JSON_obj = json.dumps( self.small_groups[ID]._asdict(), ensure_ascii=False )
file.write( unicode( JSON_obj ) )
i += 1
print str( i ) + ' small group JSON objects successfully written to the file ' + \
file.name + '.'
Here small_groups
is an ordered dictionary of a named tuple object called SmallGroup
with a key ID
which is a tuple of form (N,M)
where N,M
are positive integers, and if ID in small_groups.keys()
then small_groups[ID]._asdict()
is an ordered dictionary. Here is an example for ID=(36,1)
:
OrderedDict([('desc', ''), ('order', 36), ('GAP_ID', (36, 1)), ('GAP_desc', ''), ('GAP_pickled_ID', ''), ('is_abelian', None), ('is_cyclic', None), ('is_simple', None), ('is_nilpotent', None), ('nilpotency_class', None), ('is_solvable', None), ('derived_length', None), ('is_pgroup', None), ('generators', None), ('char_degrees', '[[1,4],[2,8]]'), ('D3', 68), ('MBS_STPP_param_type', (36, 1, 1)), ('Beta0', 36)])
The JSON output in the file looks squashed, no commas between the objects, and no opening and closing braces. It looks like this
{ object1 }{ object 2 }.....
...........{ object n }.....
Is this a valid JSON format, or do I have to separate the objects using commas?
Also, if I have a schema somewhere is there a way of validating the output against it?