def runMongoCmdReturnJson(cmd, subcommand, trace=False): """Run mongodb subcommand and return response.""" assert(cmd) assert(isinstance(cmd, list)) assert(subcommand) assert(isinstance(subcommand, str)) retId,outs,errs=Node.stdinAndCheckOutput(cmd, subcommand) if retId is not 0: Utils.Print("ERROR: mongodb call failed. %s" % (errs)) return None outStr=Node.byteArrToStr(outs) if not outStr: return None extJStr=Utils.filterJsonObject(outStr) if not extJStr: return None jStr=Node.normalizeJsonObject(extJStr) if not jStr: return None if trace: Utils.Print ("RAW > %s"% (outStr)) if trace: Utils.Print ("JSON> %s"% jStr) try: jsonData=json.loads(jStr) except json.decoder.JSONDecodeError as _: Utils.Print ("ERROR: JSONDecodeError") Utils.Print ("Raw MongoDB response: > %s"% (outStr)) Utils.Print ("Normalized MongoDB response: > %s"% (jStr)) raise return jsonData
def runMongoCmdReturnJson(cmdArr, subcommand, trace=False): retId,outs=Node.stdinAndCheckOutput(cmdArr, subcommand) if retId is not 0: return None outStr=Node.byteArrToStr(outs) if not outStr: return None extJStr=Utils.filterJsonObject(outStr) if not extJStr: return None jStr=Node.normalizeJsonObject(extJStr) if not jStr: return None if trace: Utils.Print ("RAW > %s"% (outStr)) #trace and Utils.Print ("JSON> %s"% jStr) jsonData=json.loads(jStr) return jsonData