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

Is there an option to parent the fields of an _attributes object on the parent object. #56

Closed
michaelgwelch opened this issue Mar 22, 2018 · 4 comments

Comments

@michaelgwelch
Copy link

michaelgwelch commented Mar 22, 2018

Pardon me if that question is very poorly worded.

Basically I have an XML document where each XML element has attributes, but no child elements. So it's safe to skip the creation of an _attributes object. The attributes themselves can go right on the generated JSON object.

Example: Given

<a val="5"/>
<a val="6"/>

I'd like the output to be (Note, I can make a plural myself as in this example)

{
  "as": [
    {
      "val": "5"
    },
    {
      "val": "6"
    }
  ]
}

rather than

{
  "a": [
    {
      "_attributes": {
        "val": "5"
      }
    },
    {
      "_attributes": {
        "val": "6"
      }
    }
  ]
}
@michaelgwelch
Copy link
Author

I'm going to close this. I just realized I can easily use map to create the output I want.

@nashwaan
Copy link
Owner

@michaelgwelch , Can you share an example of how you used map as I had previously many users asked for a similar result.

@michaelgwelch
Copy link
Author

michaelgwelch commented Mar 23, 2018

@nashwaan Sure I can post the code I used. But others trying to follow my advice, my example is relatively simple.

The first thing I did was manually edit my JSON file so that instead of having something like

{
  "a": [
    {
      "_attributes": {
        "val": "5"
      }
    },
    {
      "_attributes": {
        "val": "6"
      }
    }
  ]
}

I had

  [
    {
      "_attributes": {
        "val": "5"
      }
    },
    {
      "_attributes": {
        "val": "6"
      }
    }
  ]

In other words, I removed the top level parent object, leaving me with just a JSON array. So applying map was quite simple. Assuming a file named events.json the following bit of code should do the trick (assume a filename of convert.js):

#!/usr/bin/evn node
const origEvents = require('./events.json');

const newEvents = origEvents.map( (val) => { return val["_attributes"] });

console.log(JSON.stringify(newEvents, null, 2))

Then from bash

$ chmod +x convert.js
$ ./convert.js > output.json

Note: my objects actually had dozens of fields, but for the sake of this example that isn't relevant. Just the shape of the JSON is what is relevant.

To recap:

I started with XML that had this shape:

<a val="5" />
<a val="6" />

Then I used xml-js in compact mode to get the JSON at the top of this comment. Manually removed the parent object and then ran the map function.

@1mike12
Copy link

1mike12 commented Nov 23, 2022

Although mapping works, it's annoying if your documents follow the element-attribute format, where it really makes way more sense to have the attribute kvs be added directly to the element object. Here's a quick working pr that makes this work #195

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

No branches or pull requests

3 participants