Jadi karena saya bekerja dengan SOAP API yang mengembalikan XML dan saya membutuhkan/menginginkan JSON setelah saya xml2json, respons inilah yang saya dapatkan: a Ticket! Yang saya butuhkan misalnya: deskripsi tiket judul tiket nomor tiket ... lebih banyak lagi tetapi 3 untuk tujuan demonstrasi sudah cukup

Menjadi objek JSON di bawah sangat buruk ... apa cara terbaik atau paling efisien untuk mendapatkan apa yang saya butuhkan di atas: -deskripsi tiket -judul tiket -nomor tiket

Anda dapat menemukan di JSON objek yang namanya: Deskripsi, Judul, Nomor Tiket (semua string)

Saya sudah berpikir untuk membuatnya menjadi array atau array menggunakan Object.entries(JSON)

Kemudian menemukan seberapa dalam array array ini menggunakan fungsi rekursif.

Mungkin kemudian mencoba meratakan array array ke kedalaman apa pun yang ditemukan di atas.

Tapi saya masih tetap memiliki array dan objek campuran.

TOLONG!

const flattened = [
  "declaration",
  { attributes: { version: "1.0", encoding: "utf-8" } },
  "elements",
  {
    type: "element",
    name: "soap:Envelope",
    attributes: {
      "xmlns:soap": "http://schemas.xmlsoap.org/soap/envelope/",
      "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
      "xmlns:xsd": "http://www.w3.org/2001/XMLSchema"
    },
    elements: [
      {
        type: "element",
        name: "soap:Body",
        elements: [
          {
            type: "element",
            name: "queryResponse",
            attributes: { xmlns: "http://autotask.net/ATWS/v1_6/" },
            elements: [
              {
                type: "element",
                name: "queryResult",
                elements: [
                  {
                    type: "element",
                    name: "ReturnCode",
                    elements: [{ type: "text", text: "1" }]
                  },
                  {
                    type: "element",
                    name: "EntityResults",
                    elements: [
                      {
                        type: "element",
                        name: "Entity",
                        attributes: { "xsi:type": "Ticket" },
                        elements: [
                          {
                            type: "element",
                            name: "id",
                            elements: [{ type: "text", text: "40218" }]
                          },
                          {
                            type: "element",
                            name: "UserDefinedFields",
                            elements: [
                              {
                                type: "element",
                                name: "UserDefinedField",
                                elements: [
                                  {
                                    type: "element",
                                    name: "Name",
                                    elements: [
                                      { type: "text", text: "Kaseya Alert ID" }
                                    ]
                                  }
                                ]
                              },
                              {
                                type: "element",
                                name: "UserDefinedField",
                                elements: [
                                  {
                                    type: "element",
                                    name: "Name",
                                    elements: [
                                      { type: "text", text: "Kaseya Ticket ID" }
                                    ]
                                  }
                                ]
                              },
                              {
                                type: "element",
                                name: "UserDefinedField",
                                elements: [
                                  {
                                    type: "element",
                                    name: "Name",
                                    elements: [
                                      { type: "text", text: "Logicalis ticket" }
                                    ]
                                  },
                                  {
                                    type: "element",
                                    name: "Value",
                                    elements: [{ type: "text", text: "yes" }]
                                  }
                                ]
                              }
                            ]
                          },
                          {
                            type: "element",
                            name: "AccountID",
                            attributes: { "xsi:type": "xsd:int" },
                            elements: [{ type: "text", text: "0" }]
                          },
                          {
                            type: "element",
                            name: "AllocationCodeID",
                            attributes: { "xsi:type": "xsd:int" },
                            elements: [{ type: "text", text: "29682804" }]
                          },
                          {
                            type: "element",
                            name: "CreateDate",
                            attributes: { "xsi:type": "xsd:dateTime" },
                            elements: [
                              { type: "text", text: "2019-09-24T13:56:45.47" }
                            ]
                          },
                          {
                            type: "element",
                            name: "CreatorResourceID",
                            attributes: { "xsi:type": "xsd:int" },
                            elements: [{ type: "text", text: "29682933" }]
                          },
                          {
                            type: "element",
                            name: "Description",
                            attributes: { "xsi:type": "xsd:string" },
                            elements: [
                              {
                                type: "text",
                                text:
                                  "my description"
                              }
                            ]
                          },
                          {
                            type: "element",
                            name: "DueDateTime",
                            attributes: { "xsi:type": "xsd:dateTime" },
                            elements: [
                              { type: "text", text: "2019-09-24T14:56:45.47" }
                            ]
                          },
                          {
                            type: "element",
                            name: "EstimatedHours",
                            attributes: { "xsi:type": "xsd:decimal" },
                            elements: [{ type: "text", text: "0.0000" }]
                          },
                          {
                            type: "element",
                            name: "IssueType",
                            attributes: { "xsi:type": "xsd:int" },
                            elements: [{ type: "text", text: "12" }]
                          },
                          {
                            type: "element",
                            name: "LastActivityDate",
                            attributes: { "xsi:type": "xsd:dateTime" },
                            elements: [
                              { type: "text", text: "2019-10-14T11:12:59.337" }
                            ]
                          },
                          {
                            type: "element",
                            name: "Priority",
                            attributes: { "xsi:type": "xsd:int" },
                            elements: [{ type: "text", text: "1" }]
                          },
                          {
                            type: "element",
                            name: "QueueID",
                            attributes: { "xsi:type": "xsd:int" },
                            elements: [{ type: "text", text: "29682969" }]
                          },
                          {
                            type: "element",
                            name: "AssignedResourceID",
                            attributes: { "xsi:type": "xsd:int" },
                            elements: [{ type: "text", text: "29682919" }]
                          },
                          {
                            type: "element",
                            name: "AssignedResourceRoleID",
                            attributes: { "xsi:type": "xsd:int" },
                            elements: [{ type: "text", text: "29683464" }]
                          },
                          {
                            type: "element",
                            name: "Source",
                            attributes: { "xsi:type": "xsd:int" },
                            elements: [{ type: "text", text: "4" }]
                          },
                          {
                            type: "element",
                            name: "Status",
                            attributes: { "xsi:type": "xsd:int" },
                            elements: [{ type: "text", text: "1" }]
                          },
                          {
                            type: "element",
                            name: "SubIssueType",
                            attributes: { "xsi:type": "xsd:int" },
                            elements: [{ type: "text", text: "146" }]
                          },
                          {
                            type: "element",
                            name: "TicketNumber",
                            attributes: { "xsi:type": "xsd:string" },
                            elements: [{ type: "text", text: "T20190924.0157" }]
                          },
                          {
                            type: "element",
                            name: "Title",
                            attributes: { "xsi:type": "xsd:string" },
                            elements: [
                              {
                                type: "text",
                                text:
                                  "enabling file share/external sharing in Microsoft Teams"
                              }
                            ]
                          },
                          {
                            type: "element",
                            name: "FirstResponseDueDateTime",
                            attributes: { "xsi:type": "xsd:dateTime" },
                            elements: [
                              { type: "text", text: "2019-09-24T14:26:45.47" }
                            ]
                          },
                          {
                            type: "element",
                            name: "ResolvedDueDateTime",
                            attributes: { "xsi:type": "xsd:dateTime" },
                            elements: [
                              { type: "text", text: "2019-09-24T14:56:45.47" }
                            ]
                          },
                          {
                            type: "element",
                            name: "ServiceLevelAgreementID",
                            attributes: { "xsi:type": "xsd:int" },
                            elements: [{ type: "text", text: "2" }]
                          },
                          {
                            type: "element",
                            name: "Resolution",
                            attributes: { "xsi:type": "xsd:string" }
                          },
                          {
                            type: "element",
                            name: "PurchaseOrderNumber",
                            attributes: { "xsi:type": "xsd:string" }
                          },
                          {
                            type: "element",
                            name: "TicketType",
                            attributes: { "xsi:type": "xsd:int" },
                            elements: [{ type: "text", text: "1" }]
                          },
                          {
                            type: "element",
                            name: "ChangeInfoField1",
                            attributes: { "xsi:type": "xsd:string" }
                          },
                          {
                            type: "element",
                            name: "ChangeInfoField2",
                            attributes: { "xsi:type": "xsd:string" }
                          },
                          {
                            type: "element",
                            name: "ChangeInfoField3",
                            attributes: { "xsi:type": "xsd:string" }
                          },
                          {
                            type: "element",
                            name: "ChangeInfoField4",
                            attributes: { "xsi:type": "xsd:string" }
                          },
                          {
                            type: "element",
                            name: "ChangeInfoField5",
                            attributes: { "xsi:type": "xsd:string" }
                          },
                          {
                            type: "element",
                            name: "LastCustomerVisibleActivityDateTime",
                            attributes: { "xsi:type": "xsd:dateTime" },
                            elements: [
                              { type: "text", text: "2019-10-14T11:12:59.273" }
                            ]
                          },
                          {
                            type: "element",
                            name: "HoursToBeScheduled",
                            attributes: { "xsi:type": "xsd:decimal" },
                            elements: [{ type: "text", text: "0.0000" }]
                          },
                          {
                            type: "element",
                            name: "TicketCategory",
                            attributes: { "xsi:type": "xsd:int" },
                            elements: [{ type: "text", text: "3" }]
                          },
                          {
                            type: "element",
                            name: "ExternalID",
                            attributes: { "xsi:type": "xsd:string" }
                          },
                          {
                            type: "element",
                            name: "CreatorType",
                            attributes: { "xsi:type": "xsd:int" },
                            elements: [{ type: "text", text: "1" }]
                          },
                          {
                            type: "element",
                            name: "AccountPhysicalLocationID",
                            attributes: { "xsi:type": "xsd:int" },
                            elements: [{ type: "text", text: "1" }]
                          },
                          {
                            type: "element",
                            name: "LastActivityPersonType",
                            attributes: { "xsi:type": "xsd:int" },
                            elements: [{ type: "text", text: "1" }]
                          },
                          {
                            type: "element",
                            name: "LastActivityResourceID",
                            attributes: { "xsi:type": "xsd:int" },
                            elements: [{ type: "text", text: "29682919" }]
                          }
                        ]
                      }
                    ]
                  },
                  {
                    type: "element",
                    name: "EntityResultType",
                    elements: [{ type: "text", text: "ticket" }]
                  },
                  { type: "element", name: "Errors" },
                  {
                    type: "element",
                    name: "EntityReturnInfoResults",
                    elements: [
                      {
                        type: "element",
                        name: "EntityReturnInfo",
                        elements: [
                          {
                            type: "element",
                            name: "EntityId",
                            elements: [{ type: "text", text: "40218" }]
                          },
                          {
                            type: "element",
                            name: "DatabaseAction",
                            elements: [{ type: "text", text: "None" }]
                          },
                          {
                            type: "element",
                            name: "DuplicateStatus",
                            elements: [
                              {
                                type: "element",
                                name: "Found",
                                elements: [{ type: "text", text: "false" }]
                              },
                              { type: "element", name: "MatchInfo" },
                              {
                                type: "element",
                                name: "Ignored",
                                elements: [{ type: "text", text: "false" }]
                              }
                            ]
                          },
                          { type: "element", name: "Message" }
                        ]
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
];
2
waldothedeveloper 31 Oktober 2019, 06:49

1 menjawab

Jawaban Terbaik

Anda bisa membuat fungsi yang secara rekursif melewati objek dan itu elements. Jika name disertakan dalam kumpulan nama, tambahkan itu ke larik keluaran

const flattened = ["declaration",{attributes:{version:"1.0",encoding:"utf-8"}},"elements",{type:"element",name:"soap:Envelope",attributes:{"xmlns:soap":"http://schemas.xmlsoap.org/soap/envelope/","xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance","xmlns:xsd":"http://www.w3.org/2001/XMLSchema"},elements:[{type:"element",name:"soap:Body",elements:[{type:"element",name:"queryResponse",attributes:{xmlns:"http://autotask.net/ATWS/v1_6/"},elements:[{type:"element",name:"queryResult",elements:[{type:"element",name:"ReturnCode",elements:[{type:"text",text:"1"}]},{type:"element",name:"EntityResults",elements:[{type:"element",name:"Entity",attributes:{"xsi:type":"Ticket"},elements:[{type:"element",name:"id",elements:[{type:"text",text:"40218"}]},{type:"element",name:"UserDefinedFields",elements:[{type:"element",name:"UserDefinedField",elements:[{type:"element",name:"Name",elements:[{type:"text",text:"Kaseya Alert ID"}]}]},{type:"element",name:"UserDefinedField",elements:[{type:"element",name:"Name",elements:[{type:"text",text:"Kaseya Ticket ID"}]}]},{type:"element",name:"UserDefinedField",elements:[{type:"element",name:"Name",elements:[{type:"text",text:"Logicalis ticket"}]},{type:"element",name:"Value",elements:[{type:"text",text:"yes"}]}]}]},{type:"element",name:"AccountID",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"0"}]},{type:"element",name:"AllocationCodeID",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"29682804"}]},{type:"element",name:"CreateDate",attributes:{"xsi:type":"xsd:dateTime"},elements:[{type:"text",text:"2019-09-24T13:56:45.47"}]},{type:"element",name:"CreatorResourceID",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"29682933"}]},{type:"element",name:"Description",attributes:{"xsi:type":"xsd:string"},elements:[{type:"text",text:"my description"}]},{type:"element",name:"DueDateTime",attributes:{"xsi:type":"xsd:dateTime"},elements:[{type:"text",text:"2019-09-24T14:56:45.47"}]},{type:"element",name:"EstimatedHours",attributes:{"xsi:type":"xsd:decimal"},elements:[{type:"text",text:"0.0000"}]},{type:"element",name:"IssueType",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"12"}]},{type:"element",name:"LastActivityDate",attributes:{"xsi:type":"xsd:dateTime"},elements:[{type:"text",text:"2019-10-14T11:12:59.337"}]},{type:"element",name:"Priority",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"1"}]},{type:"element",name:"QueueID",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"29682969"}]},{type:"element",name:"AssignedResourceID",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"29682919"}]},{type:"element",name:"AssignedResourceRoleID",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"29683464"}]},{type:"element",name:"Source",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"4"}]},{type:"element",name:"Status",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"1"}]},{type:"element",name:"SubIssueType",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"146"}]},{type:"element",name:"TicketNumber",attributes:{"xsi:type":"xsd:string"},elements:[{type:"text",text:"T20190924.0157"}]},{type:"element",name:"Title",attributes:{"xsi:type":"xsd:string"},elements:[{type:"text",text:"enabling file share/external sharing in Microsoft Teams"}]},{type:"element",name:"FirstResponseDueDateTime",attributes:{"xsi:type":"xsd:dateTime"},elements:[{type:"text",text:"2019-09-24T14:26:45.47"}]},{type:"element",name:"ResolvedDueDateTime",attributes:{"xsi:type":"xsd:dateTime"},elements:[{type:"text",text:"2019-09-24T14:56:45.47"}]},{type:"element",name:"ServiceLevelAgreementID",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"2"}]},{type:"element",name:"Resolution",attributes:{"xsi:type":"xsd:string"}},{type:"element",name:"PurchaseOrderNumber",attributes:{"xsi:type":"xsd:string"}},{type:"element",name:"TicketType",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"1"}]},{type:"element",name:"ChangeInfoField1",attributes:{"xsi:type":"xsd:string"}},{type:"element",name:"ChangeInfoField2",attributes:{"xsi:type":"xsd:string"}},{type:"element",name:"ChangeInfoField3",attributes:{"xsi:type":"xsd:string"}},{type:"element",name:"ChangeInfoField4",attributes:{"xsi:type":"xsd:string"}},{type:"element",name:"ChangeInfoField5",attributes:{"xsi:type":"xsd:string"}},{type:"element",name:"LastCustomerVisibleActivityDateTime",attributes:{"xsi:type":"xsd:dateTime"},elements:[{type:"text",text:"2019-10-14T11:12:59.273"}]},{type:"element",name:"HoursToBeScheduled",attributes:{"xsi:type":"xsd:decimal"},elements:[{type:"text",text:"0.0000"}]},{type:"element",name:"TicketCategory",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"3"}]},{type:"element",name:"ExternalID",attributes:{"xsi:type":"xsd:string"}},{type:"element",name:"CreatorType",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"1"}]},{type:"element",name:"AccountPhysicalLocationID",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"1"}]},{type:"element",name:"LastActivityPersonType",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"1"}]},{type:"element",name:"LastActivityResourceID",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"29682919"}]}]}]},{type:"element",name:"EntityResultType",elements:[{type:"text",text:"ticket"}]},{type:"element",name:"Errors"},{type:"element",name:"EntityReturnInfoResults",elements:[{type:"element",name:"EntityReturnInfo",elements:[{type:"element",name:"EntityId",elements:[{type:"text",text:"40218"}]},{type:"element",name:"DatabaseAction",elements:[{type:"text",text:"None"}]},{type:"element",name:"DuplicateStatus",elements:[{type:"element",name:"Found",elements:[{type:"text",text:"false"}]},{type:"element",name:"MatchInfo"},{type:"element",name:"Ignored",elements:[{type:"text",text:"false"}]}]},{type:"element",name:"Message"}]}]}]}]}]}]}],
      input = flattened[3], // get the object from index 3
      names = new Set(['Description', 'Title', 'TicketNumber']);

function filter(o, names) {
    const output = [];

    if (names.has(o.name))
        output.push(o)

    if (o.elements)
        output.push(...o.elements.flatMap(e => filter(e, names)))

    return output
}

console.log(filter(input, names))
1
adiga 31 Oktober 2019, 09:33