需要删除JSON对象中的数组

2024-07-08 08:55:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我的json对象是:{"values": {"empid": 20000, "empName": "Sourav", "empSal": 8200}} 但是我想删除"Values: "。我该怎么做?我已经用Python编写了一段代码。 在后台,它从MySQL获取流式数据并发送到Kinesis

def main():
  connection = {
    "host": "127.0.0.1",
    "port": int(sys.argv[1]),
    "user": str(sys.argv[2]),
    "passwd": str(sys.argv[3])}
  kinesis = boto3.client("kinesis",region_name='ap-south-1')
  stream = BinLogStreamReader(
            connection_settings=connection,
            only_events=[DeleteRowsEvent, WriteRowsEvent, UpdateRowsEvent],
            server_id=100,
            blocking=True,
            log_file='mysql-bin.000003',
            resume_stream=True,
        )
  for binlogevent in stream:
    for row in binlogevent.rows:
      print (json.dumps(row,cls=DateTimeEncoder)) 
      kinesis.put_record(StreamName=str(sys.argv[4]), Data=json.dumps(row,cls=DateTimeEncoder), 
                         PartitionKey="default",)

Tags: injsontrueforstreamsysconnectionkinesis
2条回答

您可以调用row['values'],它将返回values中的值

代码中的一个示例是

kinesis.put_record(StreamName=str(sys.argv[4]), Data=json.dumps(row['values'],cls=DateTimeEncoder)

如果要删除"Values: " from the string thatjson.dumps`products,只需执行替换:

json_string = json.dumps(row,cls=DateTimeEncoder)
json_string = json_string.replace("Values: ", "")

然后在该字符串上使用put_record。您的json对象是一个字典,因此您不能从中删除values:字符串/键。如果确实删除了values键,则对象将为空

相关问题 更多 >

    热门问题