Project

General

Profile

« Previous | Next » 

Revision 14595

lib/csvs.py: added JsonReader, which reads parsed JSON data as row tuples

View differences:

csvs.py
261 261
    for col in col_order: pairs.append((col, dict_.pop(col))) # ordered cols 1st
262 262
    pairs += sorted(dict_.items()) # then remaining cols in alphabetical order
263 263
    return (dicts.pair_keys(pairs), dicts.pair_values(pairs))
264

  
265
class JsonReader(Filter):
266
    '''reads parsed JSON data as row tuples
267
    @param json_data [{'col': 'value', __}, __]
268
    '''
269
    def __init__(self, json_data, col_order=[]):
270
        self.header = None
271
        
272
        def filter_(row_dict):
273
            header, row = row_dict_to_list(row_dict, col_order)
274
            
275
            if self.header == None: # 1st JSON row: header
276
                self.header = header
277
                self.next_row = row
278
                row = header
279
            elif self.next_row != None: # 1st JSON row: data
280
                row = self.next_row
281
                self.next_row = None
282
            else: # remaining JSON rows
283
                assert header == self.header # all rows must have same cols
284
            
285
            return row
286
        Filter.__init__(self, filter_, iter(json_data))
287
        self.next_row = None

Also available in: Unified diff