json_decoder Package¶
json_decoder Package¶
Recursively decodes values of entire dictionary (JSON) using base64.decodestring. Optionally ignores keys given in keys_to_skip. It’s also possible to give a custom decoder instead of base64.decodestring.
- starbase.json_decoder.json_decode(json_data, keys_to_bypass_decoding=['timestamp'], keys_to_skip=[], decoder=<function decodestring at 0x7f6891f4f578>)[source]¶
Recursively decodes values of entire dictionary (JSON) using base64.decodestring. Optionally ignores (does not include in the final dictionary) keys given in keys_to_skip.
NOTE: Whenever you can, give decoder callables, instead of strings (works faster).
NOTE: In HBase stargate $ keys represent values of the columns.
Parameters: - json_data (dict) –
- keys_to_bypass_decoding (list|tuple|set) – Keys to bypass encoding/decoding. Example value [‘timestamp’,]
- keys_to_skip (list|tuple|set) – Keys to be excluded from final dict. Example value [‘timestamp’,]
- decoder (str) – Example value ‘base64.decodestring’ or base64.decodestring (assuming that base64 module has already been imported).
Return dict: Example 1: >>> test_json_data = { >>> u'Row': [ >>> { >>> u'Cell': [ >>> {u'$': u'NDQ=', u'column': u'Y29tcG9uZW50Omlk', u'timestamp': 1369030584274}, >>> {u'$': u'MQ==', u'column': u'bWFjaGluZTppZA==', u'timestamp': 1369030584274}, >>> {u'$': u'NTUx', u'column': u'c2Vuc29yOmlk', u'timestamp': 1369030584274}, >>> {u'$': u'NjQ2', u'column': u'c2Vuc29yOm1lYXN1cmVtZW50', u'timestamp': 1369030584274}, >>> {u'$': u'VGVtcA==', u'column': u'c2Vuc29yOnR5cGU=', u'timestamp': 1369030584274}, >>> {u'$': u'UGFzY2Fs', u'column': u'c2Vuc29yOnVuaXRfb2ZfbWVhc3VyZQ==', u'timestamp': 1369030584274} >>> ], >>> u'key': u'NDk1MzczYzMtNGVkZi00OWZkLTgwM2YtMDljYjIxYTYyN2Vh' >>> } >>> ] >>> } >>> json_decode(test_json_data, keys_to_skip=['timestamp'])
- {
- u’Row’: [
- {
- u’Cell’: [
- {u’column’: ‘component:id’}, {u’column’: ‘machine:id’}, {u’column’: ‘sensor:id’}, {u’column’: ‘sensor:measurement’}, {u’column’: ‘sensor:type’}, {u’column’: ‘sensor:unit_of_measure’}
], u’key’: ‘495373c3-4edf-49fd-803f-09cb21a627ea’
}
]
}
Example 2: >>> # Assuming the `test_json_data` is the same as in example 1 >>> json_decode(test_json_data, decoder='path.to.your.own.decoder')
Example 3: >>> # Assuming the `test_json_data` is the same as in example 1 >>> json_decode(test_json_data)
- {
- u’Row’: [
- {
- u’Cell’: [
- {u’$’: ‘44’, u’column’: ‘component:id’}, {u’$’: ‘1’, u’column’: ‘machine:id’}, {u’$’: ‘551’, u’column’: ‘sensor:id’}, {u’$’: ‘646’, u’column’: ‘sensor:measurement’}, {u’$’: ‘Temp’, u’column’: ‘sensor:type’}, {u’$’: ‘Pascal’, u’column’: ‘sensor:unit_of_measure’}
], u’key’: ‘495373c3-4edf-49fd-803f-09cb21a627ea’
}
]
}