0

I am trying to execute a nodejs script with python program and get the return output.

b.js

data : {
 "1":[],
 "2":[],
 "3":[],
 "4":[],
 "5":[],
 "6":[],
 "7":[],
 "8":[],
 "9":[],
 "10":[],
 "11":[{"id":"1","domain":"www.xxxxx.com","keywords":"白银交易平台","type":"Type1","time":["11","14","15","18"]}]
}


console.log(JSON.stringify(data))

a.py

if response.exitcode == 0:
  print(response.stdout)
else:
  sys.stderr.write(response.stderr)


b'{"1":[],"2":[],"3":[],"4":[],"5":[],"6":[],"7":[],"8":[],"9":[],"10":[],"11":[{"id":"1","domain":"www.xxxx.com","keywords":"\xe7\x99\xbd\xe9\x93\xb6\xe4\xba\xa4\xe6\x98\x93\xe5\xb9\xb3\xe5\x8f\xb0","type":"Type1","time":["11","14","15","18"]}]}\n'

From official docs of naked, response.stdout is returned NakedObject. How can I convert the returned NakedObject to python dictionary ?

1
  • If security is not a concern then have you tried eval? eval(response.stdout) Commented Sep 18, 2020 at 11:30

1 Answer 1

1

Looks like you need to use json module.

Ex:

d = b'{"1":[],"2":[],"3":[],"4":[],"5":[],"6":[],"7":[],"8":[],"9":[],"10":[],"11":[{"id":"1","domain":"www.xxxx.com","keywords":"\xe7\x99\xbd\xe9\x93\xb6\xe4\xba\xa4\xe6\x98\x93\xe5\xb9\xb3\xe5\x8f\xb0","type":"Type1","time":["11","14","15","18"]}]}'
import json
print(json.loads(d))

Output:

{u'11': [{u'keywords': u'\u767d\u94f6\u4ea4\u6613\u5e73\u53f0', u'domain': u'www.xxxx.com', u'type': u'Type1', u'id': u'1', u'time': [u'11', u'14', u'15', u'18']}], u'10': [], u'1': [], u'3': [], u'2': [], u'5': [], u'4': [], u'7': [], u'6': [], u'9': [], u'8': []}
Sign up to request clarification or add additional context in comments.

2 Comments

oh, sorry. I forgot there is line break at the end of the object b'{"1":[],"2":[],"3":[],"4":[],"5":[],"6":[],"7":[],"8":[],"9":[],"10":[],"11":[{"id":"1","domain":"www.xxxx.com","keywords":"\xe7\x99\xbd\xe9\x93\xb6\xe4\xba\xa4\xe6\x98\x93\xe5\xb9\xb3\xe5\x8f\xb0","type":"Type1","time":["11","14","15","18"]}]}\n' and then it gives me an error : json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
@Ekoar. That should not be a problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.