0

below is given XML wanted to convert into Json format using javascript,I did converted to json format through xml2js.parse but it is giving all the fields , but i want only few fields from records how can I perform

XML:

<?xml version="1.0" encoding="utf-16"?>
<Records count="2">
<Metadata>
    <FieldDefinitions>
        <FieldDefinition id="39520"  name="Initiative Risk ID" alias="Initiative_Risk_ID" />
        <FieldDefinition id="41304" name="JIRA Project Name" alias="JIRA_Project_Name" />
        <FieldDefinition id="41305" name="JIRA Epic Name" alias="JIRA_Epic_Name" />
        <FieldDefinition id="41306"  name="JIRA Issue Type" alias="JIRA_Issue_Type" />
        <FieldDefinition id="41310"  name="Jira Project ID" alias="Jira_Project_ID" />
<FieldDefinition id="41312"  name="Jira Project Description" alias="Jira_Project_Description" />
    </FieldDefinitions>
</Metadata>
<LevelCounts>
    <LevelCount id="432" guid="4e093763-91a1-4ff1-8f1b-920e936116f2" count="2" />
</LevelCounts>
<Record contentId="2337605" levelId="432" levelGuid="4e093763-91a1-4ff1-8f1b-920e936116f2" moduleId="865" parentId="0">
    <Field id="39520" type="6">2337605</Field>
    <Field id="41310" type="1">123</Field>
    <Field id="41304" type="1">TPA</Field>
    <Field id="41306" type="1">10002</Field>
    <Field id="41305" type="1">Epic-A</Field>
<Field id="41312" type="1">description1</Field>
</Record>
<Record contentId="2337614" levelId="432" levelGuid="4e093763-91a1-4ff1-8f1b-920e936116f2" moduleId="865" parentId="0">
    <Field id="39520" type="6">2337614</Field>
    <Field id="41310" type="1">234</Field>
    <Field id="41304" type="1">TPA</Field>
    <Field id="41306" type="1">10001</Field>
    <Field id="41305" type="1">Epic-B</Field>
<Field id="41312" type="1">description2</Field>
</Record>
</Records>

I got an output like this when it converted to json using function

XMLStringToJSON (xml, callback) {
  xml2js.parseString(xml, callback)
}

Expected Output:

{"issueUpdates":[
{"fields":
        {"project":{"key":"TPA"},
         "summary":"IRA- Created today with description1",
         "issuetype":{"id":"10002"},
         "description": {
      "type": "doc",
      "version": 1,
      "content": [
        {
          "type": "paragraph",
          "content": [
            {
              "text": "description1",
              "type": "text"
            }
          ]
        }
      ]
    }    
        }
},
{"fields":
        {"project": {"key":"TPA"},
         "summary":"IRA- Created today with description2",
        
         "issuetype":{"id":"10002"},
         "description": {
      "type": "doc",
      "version": 1,
      "content": [
        {
          "type": "paragraph",
          "content": [
            {
              "text": "description2",
              "type": "text"
            }
          ]
        }
      ]
    } 
}
}
]
}

Could you please help me out using javascript and xml2js library only Here is the json format after converting using xml2js

{
    "Records": {
      "$": {
        "count": "2"
      },
      "Metadata": [
        {
          "FieldDefinitions": [
            {
              "FieldDefinition": [
                {
                  "$": {
                    "id": "39520",
                    "name": "Initiative Risk ID",
                    "alias": "Initiative_Risk_ID"
                  }
                },
                {
                  "$": {
                    "id": "41304",
                    "name": "JIRA Project Name",
                    "alias": "JIRA_Project_Name"
                  }
                },
                {
                  "$": {
                    "id": "41305",
                    "name": "JIRA Epic Name",
                    "alias": "JIRA_Epic_Name"
                  }
                },
                {
                  "$": {
                    "id": "41306",
                    "name": "JIRA Issue Type",
                    "alias": "JIRA_Issue_Type"
                  }
                },
                {
                  "$": {
                    "id": "41310",
                    "name": "Jira Project ID",
                    "alias": "Jira_Project_ID"
                  }
                },
                {
                  "$": {
                    "id": "41312",
                    "name": "Jira Project Description",
                    "alias": "Jira_Project_Description"
                  }
                }
              ]
            }
          ]
        }
      ],
      "LevelCounts": [
        {
          "LevelCount": [
            {
              "$": {
                "id": "432",
                "count": "2"
              }
            }
          ]
        }
      ],
      "Record": [
        {
          "$": {
            "contentId": "2337605",
            "levelId": "432",
            "levelGuid": "4e093763-91a1-4ff1-8f1b-920e936116f2",
            "moduleId": "865",
            "parentId": "0"
          },
          "Field": [
            {
              "_": "2337605",
              "$": {
                "id": "39520",
                "type": "6"
              }
            },
            {
              "_": "123",
              "$": {
                "id": "41310",
                "type": "1"
              }
            },
            {
              "_": "TPA",
              "$": {
                "id": "41304",
                "type": "1"
              }
            },
            {
              "_": "10002",
              "$": {
                "id": "41306",
                "type": "1"
              }
            },
            {
              "_": "Epic-A",
              "$": {
                "id": "41305",
                "type": "1"
              }
            },
            {
              "_": "description1",
              "$": {
                "id": "41312",
                "type": "1"
              }
            }
          ]
        },
        {
          "$": {
            "contentId": "2337614",
            "levelId": "432",
            "levelGuid": "4e093763-91a1-4ff1-8f1b-920e936116f2",
            "moduleId": "865",
            "parentId": "0"
          },
          "Field": [
            {
              "_": "2337614",
              "$": {
                "id": "39520",
                "type": "6"
              }
            },
            {
              "_": "234",
              "$": {
                "id": "41310",
                "type": "1"
              }
            },
            {
              "_": "TPA",
              "$": {
                "id": "41304",
                "type": "1"
              }
            },
            {
              "_": "10001",
              "$": {
                "id": "41306",
                "type": "1"
              }
            },
            {
              "_": "Epic-B",
              "$": {
                "id": "41305",
                "type": "1"
              }
            },
            {
              "_": "description2",
              "$": {
                "id": "41312",
                "type": "1"
              }
            }
          ]
        }
      ]
    }
  }
Swap
  • 3
  • 4
  • looks like a [duplicate question](https://stackoverflow.com/questions/1773550/convert-xml-to-json-and-back-using-javascript) – vnetkc Nov 09 '22 at 03:32
  • 2
    Does this answer your question? [Convert XML to JSON (and back) using Javascript](https://stackoverflow.com/questions/1773550/convert-xml-to-json-and-back-using-javascript) – pigrammer Nov 10 '22 at 02:33

0 Answers0