Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert xml to json #305

Closed
Sriram1404 opened this issue Aug 10, 2021 · 9 comments
Closed

Convert xml to json #305

Sriram1404 opened this issue Aug 10, 2021 · 9 comments

Comments

@Sriram1404
Copy link

Sriram1404 commented Aug 10, 2021

I am getting the API response as following XML,
<a> <b ID="0"> <c>10020</c> <d>NEWYORK</d> <e>NY</e> </b> <b ID="1"> <c>06373</c> <d>OLDLYME</d> <e>CT</e> </b> </a>

I want to convert the above xml to json as follows,

{ "b": [ { "c": "10020", "d": "NEWYORK", "e": "NY" }, { "c": "06373", "d": "OLDLYME", "e": "CT" } ] }

I have already tried U.xmlToJson(), U.fromXmlWithoutAttributes() but unable to achieve the above expected result.(Skip the root element <a> and also skip xml element attributes("ID") ). Please let me know will this achieved by using Underscore-lib?

@javadev
Copy link
Owner

javadev commented Aug 10, 2021

You may made back conversion json to xml.

The service here https://javadev.github.io/xml-to-json/

@Sriram1404
Copy link
Author

Hi Sir, Thanks for prompt response. I won't convert back json to xml. I will convert the xml response to json and send it back to client. please let me know is there any way to achieve the expected json result (mentioned in my previous comment) by skipping the XML root element and also the attribute values?

@javadev
Copy link
Owner

javadev commented Aug 10, 2021

To get json

{
   "b": [
      {
         "c": "10020",
         "d": "NEWYORK",
         "e": "NY"
      },
      {
         "c": "06373",
         "d": "OLDLYME",
         "e": "CT"
      }
   ]
}

you need this XML

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <b>
    <c>10020</c>
    <d>NEWYORK</d>
    <e>NY</e>
  </b>
  <b>
    <c>06373</c>
    <d>OLDLYME</d>
    <e>CT</e>
  </b>
</root>

@javadev
Copy link
Owner

javadev commented Aug 10, 2021

This XML

<a>
   <b ID="0">
      <c>10020</c>
      <d>NEWYORK</d>
      <e>NY</e>
   </b>
   <b ID="1">
      <c>06373</c>
      <d>OLDLYME</d>
      <e>CT</e>
   </b>
</a>

will be converted to this json

{
  "a": {
    "b": [
      {
        "-ID": "0",
        "c": "10020",
        "d": "NEWYORK",
        "e": "NY"
      },
      {
        "-ID": "1",
        "c": "06373",
        "d": "OLDLYME",
        "e": "CT"
      }
    ]
  },
  "#omit-xml-declaration": "yes"
}

@Sriram1404
Copy link
Author

Thanks sir for spending yours valuable time and let me check whether i could use the xml to json conversion as it is.

@javadev
Copy link
Owner

javadev commented Aug 10, 2021

I may add a new mode to the xmlToJson method REMOVE_FIRST_LEVEL_XML_TO_JSON. Will it be good for you?

@Sriram1404
Copy link
Author

Sriram1404 commented Aug 10, 2021

Yes sir, it will be really helpful for me. Sir, also please let me know how long this change will take time to complete ?

@Sriram1404 Sriram1404 reopened this Aug 11, 2021
@javadev
Copy link
Owner

javadev commented Aug 17, 2021

Implemented #307

@javadev javadev added this to the 1.69 milestone Aug 17, 2021
@javadev javadev closed this as completed Aug 17, 2021
@javadev
Copy link
Owner

javadev commented Sep 1, 2021

The new version was released today. 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants