Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import pandas as pd 

2import json 

3import logging 

4 

5class SDCJsonFileHelpers(): 

6 """ 

7 Factory class used to easily define a custom functions to Convert a json file object to an SDCDataframe 

8 """ 

9 @staticmethod 

10 def apply_custom_pre_processing_function(file_obj_, function_name_, opts_): 

11 """ 

12 Apply a custom function to Convert a json file object to an SDCDataframe. 

13 :param function_name_: Name of function to generate a SDCDataframe. 

14 :param file_obj_: Json object to apply a custom function. 

15 :return: A fully processed SDCDataframe. 

16 """ 

17 if function_name_ in available_functions.keys(): 

18 return available_functions[function_name_](file_obj_, **opts_) 

19 else: 

20 logging.exception("%s is not a valid function option.", function_name_) 

21 raise Exception(f"{function_name_} is not a valid function option.") 

22 

23def normalize_json(file_obj_, **opts_): 

24 file_obj_.seek(0) 

25 json_obj = json.loads(file_obj_.read()) 

26 pandas_normalized_df = pd.io.json.json_normalize(json_obj, **opts_) 

27 return pandas_normalized_df 

28 

29available_functions = { 

30 'normalize_json': normalize_json, 

31}