dayannex commited on
Commit
d7d9984
·
1 Parent(s): 4311f3f

update app

Browse files
Files changed (1) hide show
  1. app.py +42 -7
app.py CHANGED
@@ -327,18 +327,53 @@ class utilJSON:
327
  for key in data:
328
  out.append(key)
329
  return(out)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
330
 
331
  def obtener_dataframe(self,data):
332
  claves=self.obtener_keys_json(data)
 
333
  if len(claves)==1:
334
- df = pd.json_normalize(
335
- data=data[claves[0]],
336
- #meta=['title', 'author'],
337
- #record_path='reviews',
338
- #errors='raise'
339
- )
340
  else:
341
- df = pd.json_normalize(data)
 
 
342
 
343
  return df
344
  modelo = ModeloDataset()
 
327
  for key in data:
328
  out.append(key)
329
  return(out)
330
+ ###
331
+ ### funcion "flatten_json" tomada de https://levelup.gitconnected.com/a-deep-dive-into-nested-json-to-data-frame-with-python-69bdabb41938
332
+ ### Renu Khandelwal Jul 23, 2023
333
+ def flatten_json(self,y):
334
+ try:
335
+ out = {}
336
+
337
+ def flatten(x, name=''):
338
+ if type(x) is dict:
339
+ for a in x:
340
+ flatten(x[a], name + a + '_')
341
+ elif type(x) is list:
342
+ i = 0
343
+ for a in x:
344
+ flatten(a, name + str(i) + '_')
345
+ i += 1
346
+ else:
347
+ out[name[:-1]] = x
348
+
349
+ flatten(y)
350
+ return out
351
+ except json.JSONDecodeError:
352
+ print("Error: The JSON document could not be decoded.")
353
+ except TypeError:
354
+ print("Error: Invalid operation or function argument type.")
355
+ except KeyError:
356
+ print("Error: One or more keys do not exist.")
357
+ except ValueError:
358
+ print("Error: Invalid value detected.")
359
+ except Exception as e:
360
+ # Catch any other exceptions
361
+ print(f"An unexpected error occurred: {str(e)}")
362
 
363
  def obtener_dataframe(self,data):
364
  claves=self.obtener_keys_json(data)
365
+ print(claves)
366
  if len(claves)==1:
367
+
368
+ #Flatten nested dictionaries and lists
369
+ data_flattened = [self.flatten_json(class_info) for class_info in data[claves[0]]]
370
+ # Create DataFrame from flattened JSON
371
+ df = pd.DataFrame(data_flattened)
372
+
373
  else:
374
+ #df = pd.json_normalize(data)
375
+ data_flattened = [self.flatten_json(class_info) for class_info in data]
376
+ df = pd.DataFrame(data_flattened)
377
 
378
  return df
379
  modelo = ModeloDataset()