2016-04-16 4 views
0

I verwendet mongoimport -d test -c BB --file=bb.jsonInsert JSON-Datei in MongoDB - JSON hat Metadaten und eine weitere Liste mit den aktuellen Daten

auch versuchte ich mongoimport -d test -c BB --jsonArray --file=bb.json

sie nur als einen Datensatz einfügen, anstatt mehrere Datensätze (2900 ca.)

Ich weiß, dass in MongoDB die Daten im Schlüssel: Wert-Paar sein sollten und diese Daten sind in der Liste für aber mit Metadaten auch. Kann ich Daten in diesem Formular selbst importieren, ohne sie in Schlüssel: Wert-Paare umzuwandeln?

Dank Hier die Daten oder Informationen aus Eingabedatei bb.json die ich

---------------------------------------------------------------------- 
Start of DATA File 
{ "data": [ 
     [ 
      "1", 
      "Andaman and Nicobar Islands", 
      "NA", 
      "Port Blair", 
      "G.B Pant Hospital", 
      "Atlanta Point", 
      "744104", 
      "03192 230628", 
      "NA", 
      "NA", 
      "NA", 
      "NA", 
      "NA", 
      "NA", 
      "NA", 
      "NA", 
      "[=12]12", 
      "[=93]93" 
     ], 
     [ 
      "2", 
      "Andaman and Nicobar Islands", 
      "NA", 
      "Port Blair", 
      "I.N.H.S. Dhanvantri", 
      "Minni Bay", 
      "744103", 
      "03192 248759", 
      "NA", 
      "NA", 
      "NA", 
      "NA", 
      "NA", 
      "NA", 
      "NA", 
      "NA", 
      "NA", 
      "NA" 
     ], 
..... # RECORDS 
     [ 
      "2946", 
      "West Bengal", 
      "NA", 
      "Murshidabad", 
      "Lalbagh S.D. Hospital Blood Bank", 
      "P.O. Lalbagh", 
      "742149", 
      "03482 270247", 
      "NA", 
      "NA", 
      "NA", 
      "NA", 
      "NA", 
      "NA", 
      "NA", 
      "NA", 
      "[=24]24", 
      "[=88]88" 
     ] 
    ], 
    "fields": [ 
     { "id": "a", "label": "id", "type": "string" }, 
     { "id": "b", "label": "state", "type": "string" }, 
     { "id": "c", "label": "city", "type": "string" }, 
     { "id": "d", "label": "district", "type": "string" }, 
     { "id": "e", "label": "h_name", "type": "string" }, 
     { "id": "f", "label": "address", "type": "string" }, 
     { "id": "g", "label": "pincode", "type": "string" }, 
     { "id": "h", "label": "contact", "type": "string" }, 
     { "id": "i", "label": "helpline", "type": "string" }, 
     { "id": "j", "label": "fax", "type": "string" }, 
     { "id": "k", "label": "category", "type": "string" }, 
     { "id": "l", "label": "website", "type": "string" }, 
     { "id": "m", "label": "email", "type": "string" }, 
     { "id": "n", "label": "blood_component", "type": "string" }, 
     { "id": "o", "label": "blood_group", "type": "string" }, 
     { "id": "p", "label": "service_time", "type": "string" }, 
     { "id": "q", "label": "latitude", "type": "string" }, 
     { "id": "r", "label": "longitude", "type": "string" } 
    ] 
} 

END of DATA File ----------------------------------------------------------------------- 

Antwort

0

Ich schrieb nur ein kleines Python-Programm zu importieren versuchen es zu geeignetem JSON-Format zu konvertieren, die MongoDB konnte Verwenden Sie, um nach MongoDB zu importieren.

Hier ist sie:

# https://docs.python.org/2/library/pprint.html 
# On Command Line use "cat some.json | python -m json.tool" 

import pprint 
import json 


fh = open ('Blood_bank_updated-sep_2015.json', 'r') 

# json_bb1 = json.dumps(json_bb, indent = 4, sort_keys = False/True) 

# To parse a STRING use "json.loads" 
# To parse a FILE use "json.load" 
#  Please note the difference is load[s] - s for string 

json_bb = json.load(fh) 

print 'json_bb :', type(json_bb) 


var_i = 0 
for key in json_bb.keys(): 
    var_i = var_i + 1 
    print var_i, key 

fields = range (len(json_bb['fields'])) 

var_i = 0 
for label in json_bb['fields']: 
    fields[var_i] = label['label'] 
    var_i = var_i + 1 
    # print var_i, label 
    # print label['label'] 

# Change "id" to "_id" for MongoDB "_id" 
if fields[0] == "id": 
    fields[0] = "_id" 

print "fields :", fields 

fhand = open("BB_MongoDB.json", 'w') 

var_i = 0 
for values in json_bb['data']: 
    rec = list() 
    for index in range(len(fields)): 

    rec.append((fields[index], values[index])) 

    temp_data = dict(rec) 

    temp_data2 = json.dumps(temp_data, sort_keys = True) 

    fhand.write(temp_data2 + u"\n") 

    if var_i < 22: 
     print var_i, "Values :", values, "\n\n" 
     print "Index :", index, "Record :" , rec 
     print "\n\n\nIndex :", var_i, "Mongo_BB :" , bb_mongo 
    var_i += 1 


quit()