1

I have a json file with non valid lines. I read it using this code

import json
import pandas as pd
data = []
with open('json file ') as f:
   for line in f: 
      data.append(json.loads(line))

Sorry about the ugly looking code, I' m using the mobile Stack Exchange app. What I would like to do is to convert the data object into a data frame which columns are the first 5 elements of each data object list. Can you help? Cheers!

Dani

Amit
  • 19,780
  • 6
  • 46
  • 54
Dani Valverde
  • 407
  • 8
  • 24
  • Pandas has a read_json() that will return a df with a number of parameters depending on your json structure. – Bob Haffner Dec 23 '14 at 18:42
  • But that probably won't work if the JSON is invalid. The OP seems to assume that a single line of input will be a self-contained piece of JSON, an assumption that is almost bound to be incorrect. It's hard to suggest anything except fixing the JSON source. – holdenweb Dec 23 '14 at 18:45
  • Thanks. I've tried with pd.read_json(data) but I get type error expected string or Unicode – Dani Valverde Dec 23 '14 at 18:46

1 Answers1

6

I feel a little bit ashamed. It is as easy as using the Dataframe method:

df = pd.DataFrame(data)

Dani Valverde
  • 407
  • 8
  • 24