简体   繁体   English

如何使用节点 js 或 python 将带有转义字符串的 json 解析为更小的 json

[英]How to parse a json with escaped string into smaller json with node js or python

I'm using AWS step functions and want to parse the following json with a node js or python lambda function (the language is irrelevant, I just need the results): I'm using AWS step functions and want to parse the following json with a node js or python lambda function (the language is irrelevant, I just need the results):

{
  "stage": "dev",
  "server": {
    "instanceId": "i-xxxx",
    "status": "running"
  },
  "message": [
    {
      "MessageId": "xxx",
      "ReceiptHandle": "xxx",
      "MD5OfBody": "xxx",
      "Body": "{\n  \"stage\": \"DEV\",\n  \"id\": \"5\",\n \"results\": \"D:\\\\Temp\\\\results\"\n}",
      "Attributes": {
        "SenderId": "xxxx",
        "ApproximateFirstReceiveTimestamp": "xxx",
        "ApproximateReceiveCount": "1",
        "SentTimestamp": "xxx",
        "SequenceNumber": "xxx",
        "MessageDeduplicationId": "20",
        "MessageGroupId": "20"
      }
    }
  ]
}

I just need the "message.Body"-Part.我只需要“message.Body”部分。 I'd like to split this into three vars for further actions:我想将其拆分为三个变量以进行进一步操作:

{
  "stage":"DEV",
  "id":"5",
  "results":"D:\\Temp\\results"
}

Could someone help me with this?有人可以帮我解决这个问题吗? I just spent the last hour with JSON.stringify, parse, unescape and so on, unfortunately without success:(我刚刚花了最后一个小时 JSON.stringify, parse, unescape 等等,不幸的是没有成功:(

Thank you in advance!先感谢您!

Does this work?这行得通吗?

import json
jsondata = {
  "stage": "dev",
  "server": {
    "instanceId": "i-xxxx",
    "status": "running"
  },
  "message": [
    {
      "MessageId": "xxx",
      "ReceiptHandle": "xxx",
      "MD5OfBody": "xxx",
      "Body": "{\n  \"stage\": \"DEV\",\n  \"id\": \"5\",\n \"results\": \"D:\\\\Temp\\\\results\"\n}",
      "Attributes": {
        "SenderId": "xxxx",
        "ApproximateFirstReceiveTimestamp": "xxx",
        "ApproximateReceiveCount": "1",
        "SentTimestamp": "xxx",
        "SequenceNumber": "xxx",
        "MessageDeduplicationId": "20",
        "MessageGroupId": "20"
      }
    }
  ]
}
jsonbody = json.loads(jsondata.get('message')[0].get('Body'))
print(jsonbody)
# {'stage': 'DEV', 'id': '5', 'results': 'D:\\Temp\\results'}

Here is my way of implementing it in Python,这是我在 Python 中实现它的方式,

import json

dic = {
  "stage": "dev",
  "server": {
    "instanceId": "i-xxxx",
    "status": "running"
  },
  "message": [
    {
      "MessageId": "xxx",
      "ReceiptHandle": "xxx",
      "MD5OfBody": "xxx",
      "Body": "{\n  \"stage\": \"DEV\",\n  \"id\": \"5\",\n \"results\": \"D:\\\\Temp\\\\results\"\n}",
      "Attributes": {
        "SenderId": "xxxx",
        "ApproximateFirstReceiveTimestamp": "xxx",
        "ApproximateReceiveCount": "1",
        "SentTimestamp": "xxx",
        "SequenceNumber": "xxx",
        "MessageDeduplicationId": "20",
        "MessageGroupId": "20"
      }
    }
  ]
}

message_body_str = dic['message'][0]['Body']
print(json.dumps(json.loads(message_body_str)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM