使用 Jolt 从 MSFT 响应中过滤掉特定值

Use Jolt to filter out specific values from MSFT response

我从 GraphAPI 收到以下(预过滤)响应,但仍有一些我不想要的值。我想让它尽可能简单,但不确定哪种方法最好。我尝试过的一种方法已发布在此处。

{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('mailbox%40mail.app')/messages(sender,subject,body)",
  "value": [
    {
      "@odata.etag": "W/\"6xAAAAAAKq\"",
      "id": "6xAAAAAAVQAAA=",
      "subject": "Can we speak tomorrow?",
      "body": {
        "contentType": "text",
        "content": "Meeting at 10?"
      },
      "sender": {
        "emailAddress": {
          "name": "test-mail",
          "address": "test-mail@sender.mail.app"
        }
      }
    },
    {
      "@odata.etag": "W/\"6xAAAAAAKq\"",
      "id": "6xAAAAAAVQAAA=",
      "subject": "Meet for lunch?",
      "body": {
        "contentType": "text",
        "content": "The new cafeteria is open."
      },
      "sender": {
        "emailAddress": {
          "name": "test-mail",
          "address": "test-mail@sender.mail.app"
        }
      }
    },
    {
      "@odata.etag": "W/\"6xAAAAAAKq\"",
      "id": "6xAAAAAAVQAAA=",
      "subject": "Nice city",
      "body": {
        "contentType": "text",
        "content": "Want to join me on a business trip?"
      },
      "sender": {
        "emailAddress": {
          "name": "test-mail",
          "address": "test-mail@sender.mail.app"
        }
      }
    }
  ]
}

结果应该是这样的:

{
  "Messages": [
    {
      "id": "6xAAAAAAVQAAA=",
      "sender": "test-mail@sender.mail.app",
      "subject": "Can we speak tomorrow?",
      "content": "Meeting at 10?"
    },
    {
      "id": "6xAAAAAAVQAAA=",
      "sender": "test-mail@sender.mail.app",
      "subject": "Meet for lunch?",
      "content": "The new cafeteria is open."
    },
    {
      "id": "6xAAAAAAVQAAA=",
      "sender": "test-mail@sender.mail.app",
      "subject": "Nice city",
      "content": "Want to join me on a business trip?"
    }
  ]
}

我尝试了以下方法,但无法真正理解它。

[
  {
    "operation": "shift",
    "spec": {
      "value": {
        "*": {
          "id": "value.[&1].id",
          "sender": "value.[&1].sender.emailAddress.address",
          "subject": "value.[&1].subject",
          "body": "value.[&1].sender.emailAddress.address"
        }
      }
    }
  }
]

遍历移位转换规范中 value 列表的索引是个好主意。您可以更喜欢通过 @(0,elm_at_surr_level.sub_elm_level1.sub_elm_level2) 引用作为键来寻址不在当前对象中的属性,例如

[
  {
    "operation": "shift",
    "spec": {
      "value": {
        "*": {
          "id": "Messages.[&1].&",
          "@(0,sender.emailAddress.address)": "Messages.[&1].sender",
          "subject": "Messages.[&1].&",
          "@(0,body.content)": "Messages.[&1].content"
        }
      }
    }
  }
]

站点 http://jolt-demo.appspot.com 上的 演示

如果按显示排序(顺序为 id->sender->subject->content ) 需要,那么这会有点棘手,例如

[
  {
    "operation": "shift",
    "spec": {
      "value": {
        "*": {
          "id": "Messages.[&1].&",
          "@(0,sender.emailAddress.address)": "Messages.[&1].sender",
          "subject": "Messages.[&1].&",
          "@(0,body.content)": "Messages.[&1].zontent"
        }
      }
    }
  },
  {
    "operation": "sort"
  },
  {
    "operation": "shift",
    "spec": {
      "Messages": {
        "*": {
          "*": "&2.[&1].&",
          "z*": "&2.[&1].c&(0,1)"
        }
      }
    }
  }
]