0

I'm not sure why this happen, there are zero length in my json file.

0

I t supposedly to be like this,

1000

I'm afraid the comma thing after each json object cause this issue. (My current json format)

{ A:"A"},{ B:"B"),...

The correct way is like this

{ A:"A"} { B:"B"),...

So how I can calculate all the length without removing the comma?

My code

import json

githubusers_data_path = 'githubusers.json'

githubusers_data = []
githubusers_file = open(githubusers_data_path, "r")
for line in githubusers_file:
    try:
        data = json.loads(line)
        githubusers_data.append(data)
    except:
        continue

print len(githubusers_data)

Sample

{
    "login": "datomnurdin"
}, {
    "login": "ejamesc"
},...
halfer
  • 19,824
  • 17
  • 99
  • 186
Nurdin
  • 23,382
  • 43
  • 130
  • 308
  • 1
    I have no idea what you are trying to do but your code fails on `data = json.loads(line)`. This is because you read the file line by line and none of the lines `{`, `"login": "datomnurdin"`, `}, {`, etc. are valid JSONs. I suppose you wanted to do `data = json.load(githubusers_file)` (note no "s" in "load"). – freakish Nov 25 '15 at 07:49
  • Verify that your json is valid by running it through a [validator](http://jsonlint.com/) – Kamehameha Nov 25 '15 at 07:50
  • @freakish Already test it. still 0 length. – Nurdin Nov 25 '15 at 07:51
  • @Kamehameha It's valid 101%. – Nurdin Nov 25 '15 at 07:51
  • The sample json you posted isn't valid json. I ran it through http://jsonlint.com/ (removed the dots obviously). – Arnab Datta Nov 25 '15 at 08:16
  • @ArnabDatta I'm not sure it's a sarcasm or not, the 'dot' thing shows more than 2 json objects. I hope u understand that. – Nurdin Nov 25 '15 at 08:19
  • @Dato'MohammadNurdin : nope, no sarcasm. The validator claimed it wasn't valid json. I think for it to be valid json, you need one top-level object only and if it's in the format you described, then you have more than one. – Arnab Datta Nov 25 '15 at 09:08
  • In the future, use `except Exception as e` and print `e` for information when it fails. – Stian OK Nov 25 '15 at 09:33

2 Answers2

2

I think you're getting an exception that you're suppressing with try-except, because of the commas. One solution would be to convert your file to a string first, stick a '[' and ']' around the string to convert it into a valid json format, then use json.loads to convert the string.

import json

githubusers_data_path = 'githubusers.json'

githubusers_file = open(githubusers_data_path, "r")
githubusers_string = ''.join(line for line in githubusers_file)
githubusers_string = '[{}]'.format(githubusers_string)
githubusers_data = json.loads(githubusers_string)

print len(githubusers_data)
githubusers_file.close()
zehnpaard
  • 6,003
  • 2
  • 25
  • 40
0

there is an exception in your code:

import json

githubusers_data_path = 'githubusers.json'

githubusers_data = []
githubusers_file = open(githubusers_data_path, "r")
for line in githubusers_file:
    try:
        data = json.load(githubusers_file) # exception from here
        githubusers_data.append(data)
    except Exception, e:
        print e

print len(githubusers_data) # so githubusers_data is always []

Mixing iteration and read methods would lose data

Community
  • 1
  • 1
FelixHo
  • 1,254
  • 14
  • 26
  • your suggestion I got Mixing iteration and read methods would lose data error message. but the length still 0. – Nurdin Nov 25 '15 at 08:42