-1

Are my expectations incorrect? output desired is for a this single field

"{\"escapeChar\":\"\\\", \"quoteChar\":\"\"\", \"separatorChar\":\",\"}"

input csv text

"{""escapeChar"":""\\"", ""quoteChar"":""\"""", ""separatorChar"":"",""}"

Code:

obj_str = json.dumps(table_obj).replace('RS Target Table', 'Table Name').replace('Stage Table', 'input_table')

#Get obj back with replacement  BANG!!
obj = json.loads(obj_str)

print ('after loads, source_serde_params: ',   obj['source_serde_params'])
with open(filename, 'w' ) as write_file:
                    json.dump(obj, write_file,  indent=2)

console:

after loads, source_serde_params:  {"escapeChar":"\\", "quoteChar":"\"", "separatorChar":","}

output file:

"source_serde_params": "{\"escapeChar\":\"\\\\\", \"quoteChar\":\"\\\"\", \"separatorChar\":\",\"}"

python 3.7.9 windows laptop

Parzival
  • 1,666
  • 3
  • 9
  • 25

1 Answers1

0

Th output is correct - it is just that you are seeing the output in an environment which is shwoing you any actual \ in the data so that it can reuse the \ character for denoting escape sequences for other charactes like ',",n,t etc...

Just change the way you are looking at the output file to see its actual contents. As you write that you are seeing that in the "console", you may be seeing the interactive output, which shows you the repr of the string, where the backlash is used in this way; Just put a print (...) around your encoded json string to see its actual, unescaped, contents.

jsbueno
  • 86,446
  • 9
  • 131
  • 182