1

I hope I've set an appropriate "title" for this question. Let me share a mashed-up screenshot to show you what I mean:

I have JSON formatted output on the left (A) as a result of the conversion of XML data. I found that I needed to specifically include the "element" notation to have the structure of the file properly created (must have to do with the way that I'm dynamically looping and creating my XML). However, the actual JSON formatted output structure that I need is on the right (B). Somehow I need to drop the "element" notation... sort of "collapse" it I guess. (Again, I'm trying to avoid rewriting the code that creates the XML and instead, be able to convert my XML to the JSON that I need.)

OK. So it appears that I'm not allowed to post an image, so I'll try to mock something here up in text instead.

Here's what I'm getting:

JSON
 productCategoriesList
  element
  {}
   id=1
   name=product type A
   products
    element
     {}
      id=A1
      name=product A1
     {}
      id=A2
      name=product A2
  {}
   id=2
   name=product type B
   products
    element
     {}
      id=B1
      name=product B1

etc.

This is what I need:

JSON
 productCategoriesList
  {}
   id=1
   name=product type A
   products
     {}
      id=A1
      name=product A1
     {}
      id=A2
      name=product A2
  {}
   id=2
   name=product type B
   products
     {}
      id=B1
      name=product B1

etc.

(I probably don't need to tell you that I'm a bit of a hack. As such, I appreciate your assistance and your humility as you accept my hacky-ness.)

Thanks!

  • possible duplicate of [Eliminate duplicates in array (JSONiq)](http://stackoverflow.com/questions/19618767/eliminate-duplicates-in-array-jsoniq) – Paul Sweatte Jan 28 '14 at 20:51

1 Answers1

0

I don't see a screenshot, so I'm mostly guessing what you're trying to do and what your data looks like, but could you recurse through the JSON structure looking for any nodes containing an "element" key, replacing the node with the contents of its "element"?

Since you also did not state what language you are working in, I'll use JavaScript.

function collapse_elements(tree) {
    // Helper function to do the recursion
    function _collapse(node, parent, key) {
        // we only need to modify this node if it can have children
        if (typeof node === 'object') {
            if (typeof node['element'] !== 'undefined' && parent) {
                // If this node contains an 'element' property,
                // replace this node with the value of 'element'.
                node = node['element'];
                parent[key] = node;
            }

            // Recursively apply this to all child nodes
            for (var k in node) {
                if (node.hasOwnProperty(k)) {
                    _collapse(node[key], node, k);
                }
            }
        }
    }

    _collapse(tree, null, null);
    return tree;
}
Joel Spadin
  • 436
  • 3
  • 7
  • Thanks Joel. Sorry, but I discovered that I wasn't able to add a screenshot since I'm a "newbie". I tried to mimic my screenshot by providing a couple of text only examples. I'm using ColdFusion, but Javascript should be fine as I can incorporate that into my page. What you say above makes sense... it's just a small matter of me actually getting it to work now :o) Forgive me, but "tree" in your example above would be my converted JSON structure, right? Thanks again. – user3093109 Dec 11 '13 at 23:30
  • Yep. I haven't rigorously tested that at all, but it worked for the few simple JSON structures I threw at it. It will, of course, break things if you happen to have anything named "element" that you don't want to collapse though. – Joel Spadin Dec 12 '13 at 14:52