pythonjsonhappybase

Writing contents to json file which already has headers


So I am experimenting with happybase and I want to write the contents of a scan sequence to a json document with a skeleton I've already put in. This is the skeleton for the intended output file:

[{
   "Key": "",
   "Values": ""
}]

and from the code I hope to achieve this final format for the intended json file:

[{
   "Key":"01/01/2009",
   "Values": {
                "actual:number":30000,
                "predicted:number":40000
             }
 },
 {
   "Key":"01/02/2009",
   "Values": {
                "actual:number":30000,
                "predicted:number":40000
             }
 }]....

My Hbase Table is structured this way:

'01/01/2009','actual:number',value='30000'
'01/02/2009','predicted:number',value='40000'

and this is the code I use to access the table:

import happybase

import simplejson as sjson

import json

connection = happybase.Connection('localhost')

table = connection.table('Date-Of-Repairs')

file = open('test.json','wb+')

for key, data in table.scan(row_start='01/01/2009'):
    a = key, data
    print sjson.dumps(a)
    json.dump(a,file, indent = 2)

file.close()

I want to know how I can implement my desired json output file, and also how to stop the content written to the json to be printed out like this:

[
  "01/01/2009", 
   {
     "Actual:number": "10000", 
     "Predicted:number": "30000"
   }
][
  "01/02/2009", 
  {
    "Actual:number": "40000", 
    "Predicted:number": "40000"
  }
][
  "01/03/2009", 
   {
    "Actual:number": "50000", 
    "Predicted:number": "20000"
   }
]

As this is the current output that is being displayed in the output file


Solution

  • Thanks @anand. I figured out the answer. I just had to create a dictionary for the storing the table data, then appending to a list to be stored in a file, which yield a full json file.

    The code is below:

    import happybase
    
    import json
    
    import csv
    
    file = open('test.json','wb+')
    
    store_file = {}
    json_output = []
    
    for key, data in table.scan(row_start='01/01/2009'):
       store_file['Key'] = key
       store_file['Value'] = data
       json_output.append(store_file.copy())
       print json_output
    
    json.dump(json_output,file, indent = 2)
    
    file.close()
    

    This then yields:

    [
      {
        "Key": "01/01/2009", 
        "value": {
           "Actual:number": "10000", 
           "Predicted:number": "30000"
        }
     }, 
     {
       "Key": "01/02/2009", 
       "value": {
          "Actual:number": "40000", 
          "Predicted:number": "40000"
       }
     }, 
     {
       "Key": "01/03/2009", 
       "value": {
          "Actual:number": "50000", 
          "Predicted:number": "20000"
       }
     }
    ]
    

    I hope this helps anyone that is stuck with a problem like such.