Reputation: 1385
I'm getting json file and converting it to a yaml file with a script.
I used python code in a bash shell script as below:
#!/bin/bash
#python - ${INSTALL_DIR}/storm-tmp.json > ${INSTALL_DIR}/storm.yaml << EOF
python - ${INSTALL_DIR}/storm-tmp.json << EOF
import os
import sys
import json
with open(sys.argv[1], 'r') as f:
obj = json.load(f)
lines_to_write = []
for key in obj:
value = obj[key]
value = value.replace('\\n','\n')
print(('%s: %s')%(key, value))
EOF
I expected the output to be something like:
storm.log.dir: /logs
storm.zookeeper.servers:
- zkserver-0.zookeeper.com
- zkserver-1.zookeeper.com
- zkserver-2.zookeeper.com
storm.local.dir: /data
nimbus.seeds:
- stormnimbus-0.storm.com
But the printed output is:
storm.log.dir: /logs
storm.zookeeper.servers: \n - zkserver-0.zookeeper.com\n - zkserver-1.zookeeper.com\n - zkserver-2.zookeeper.com
storm.local.dir: /data
nimbus.seeds: \n - stormnimbus-0.storm.com
I have no idea why the \n
character is not translated as a new line character.
How can I resolve this?
EDITED
input:
{
"storm.log.dir" : "/logs",
"nimbus.seeds" : "\\n - stormnimbus-0.storm.com",
"storm.local.dir" : "/data",
"storm.zookeeper.servers" : "\\n - zkserver-0.zookeeper.com\\n - zkserver-1.zookeeper.com\\n - zkserver-2.zookeeper.com"
}
Upvotes: 0
Views: 90
Reputation:
Here is reading from the file using 'EOF' to suppress expansion (of \):
#!/bin/bash
python - 1.json <<'EOF'
import os
import sys
import json
with open(sys.argv[1], 'r') as f:
obj = json.load(f)
for k, v in obj.items():
print(('%s: %s') % (k, v.replace('\\n', '\n')))
EOF
Upvotes: 0
Reputation: 54733
Doing this kind of nested backslashes in bash is painful. You need:
value = value.replace('\\\\n','\\n')
With that, your script works for me.
By the way You can replace this:
for key in obj:
value = obj[key]
with this:
for key,value in obj.items():
Upvotes: 3