N
N
Nikolay Baranenko2017-08-24 15:34:15
Oracle
Nikolay Baranenko, 2017-08-24 15:34:15

What is the correct way to export LOB value to JOSN using cx_Oracle?

Hello.
you need to export information from Oracle and fill it in JSON

#из наименований колонок делаю словарь
for i in range(0, len(cursor.description)):
    col_names.append(cursor.description[i][0])
    pp = pprint.PrettyPrinter(width=1024)

#далее разбираю курсор для JSON
for row in cursor:
    count_rows+=1
    row_content_json=[]
    for col, val in zip(col_names, row):
        result_inside[col] = val
        row_content_json.append(result_inside[col])
        # print (col," :: ",val)
    file.write(json.dumps(row_content_json, default = myconverter))

At the output, instead of the values ​​of the LOB fields, .
How to properly export LOB to JSON?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolay Baranenko, 2017-08-24
@drno-reg

found such a solution, a question with the conditionality of checking the presence of the read attribute

for row in cursor.fetchall():
    count_rows+=1
    result_inside={}
    row_content=[]
    for col, val in zip(col_names, row):
        # проверяем наличие атрибута у объекта, если CLOB то приеняем read
        if hasattr(val, 'read'):
            result_inside[col] = val.read()
        else:
            result_inside[col] = val
        row_content.append(result_inside[col])
    json_file.write(json.dumps(result_inside, default = myconverter))

the solution above has a problem
Internally, Oracle uses LOB locators which are allocated based on the
cursor array size. Thus, it is important that the data in the LOB
object be manipulated before another internal fetch takes place. The
safest way to do this is to use the cursor as an iterator. In
particular, do not use the fetchall() method.
That's why
for row in cursor:
    count_rows+=1
    result_inside={}
    row_content=[]
    for col, val in zip(col_names, row):
        # проверяем наличие атрибута у объекта, если CLOB то приеняем read
        if hasattr(val, 'read'):
            result_inside[col] = val.read()
        else:
            result_inside[col] = val
        row_content.append(result_inside[col])
    json_file.write(json.dumps(result_inside, default = myconverter))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question