0

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

Will Vousden
  • 32,488
  • 9
  • 84
  • 95
moodygeek
  • 127
  • 13

2 Answers2

0

Python json library uses standard formatting for JSON text it has indent parameter which can help you to control the number of spaces that should be considered for indentation.

json.dumps(pydict, indent=4)

If you need a compressed JSON, which is good to use in production you can simply ignore it and python will generate minified text which reduces network traffic and parsing time.

If you want your way to print then you will have to implement it on your own, there is a module called pjson you can take it as an example of how to do it.

anand
  • 1,506
  • 14
  • 28
0

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.

moodygeek
  • 127
  • 13