splunk-sdk-python icon indicating copy to clipboard operation
splunk-sdk-python copied to clipboard

Loading data from JSON throws error when values are unicode

Open ruxandraburtica opened this issue 8 years ago • 1 comments

Problem:

I've created a Splunk job using the general Splunk examples and I'm waiting for the data (continually checking to see if the job finished):

        job = jobs.create(search, **search_kwargs)
        print('Generated job with SID: {}\n\n'.format(job.sid))

        while True:
            while not job.is_ready():
                pass
            stats = {'isDone': job['isDone'],
                     'doneProgress': float(job['doneProgress']) * 100,
                     'scanCount': int(job['scanCount']),
                     'eventCount': int(job['eventCount']),
                     'resultCount': int(job['resultCount'])}

            status = ('\r%(doneProgress)03.1f%%   %(scanCount)d scanned   '
                      '%(eventCount)d matched   %(resultCount)d results') % stats

            sys.stdout.write(status)
            sys.stdout.flush()
            if stats['isDone'] == '1':
                sys.stdout.write('\n\nDone!\n\n')
                break
            sleep(2)

        results_iterator = job.results(count=200)
        raw_data = results_iterator.read()
        entire_json_data = splunk_data.load(raw_data)

Details:

0. Version of this project: splunk-sdk==1.6.2

1. Platform version:

$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 16.04.3 LTS
Release:	16.04
Codename:	xenial

2. Framework version: Python 2.7.12

3. Splunk version: Splunk 6.5.3.1

4. Stacktrace:

  File "/work/data_processing/connectors/splunk.py", line 112, in get_and_wait_for_data
    entire_json_data = splunk_data.load(raw_data)
  File "/work/venv/lib/python2.7/site-packages/splunklib/data.py", line 83, in load
    return load_root(items[0], nametable)
  File "/work/venv/lib/python2.7/site-packages/splunklib/data.py", line 145, in load_root
    k, v = load_elem(element, nametable)
  File "/work/venv/lib/python2.7/site-packages/splunklib/data.py", line 109, in load_elem
    value = load_value(element, nametable)
  File "/work/venv/lib/python2.7/site-packages/splunklib/data.py", line 172, in load_value
    name, item = load_elem(child, nametable)
  File "/work/venv/lib/python2.7/site-packages/splunklib/data.py", line 109, in load_elem
    value = load_value(element, nametable)
  File "/work/venv/lib/python2.7/site-packages/splunklib/data.py", line 172, in load_value
    name, item = load_elem(child, nametable)
  File "/work/venv/lib/python2.7/site-packages/splunklib/data.py", line 109, in load_elem
    value = load_value(element, nametable)
  File "/work/venv/lib/python2.7/site-packages/splunklib/data.py", line 172, in load_value
    name, item = load_elem(child, nametable)
  File "/work/venv/lib/python2.7/site-packages/splunklib/data.py", line 115, in load_elem
    if isinstance(value, str):
TypeError: 'unicode' object does not support item assignment

Solution:

I've solved the problem by using basestring in load_elem instead of str so that unicode is also accepted.

ruxandraburtica avatar Feb 28 '18 16:02 ruxandraburtica

See PR #193

shakeelmohamed avatar Feb 28 '18 18:02 shakeelmohamed