1

I am using AngularJS template to show a JSON as a list/tree structure.

My JSON depth is unknown. In most of the cases my code works. There is just one case I have encountered so far that does not work.

My JSON sample:

[
   {
      "type":"image",
      "bitmapname":{
         "text":"Image_12.bmp"
      },
      "command":[
         {
            "type":"close",
            "text":"1234"
         },
         {
            "type":"place",
            "text":"company"
         },
         {
            "type":"key",
            "key": "Tab"
         },
         {
            "type":"keyboard",
            "text":"welcome",
            "command": [
               {
                  "type":"open",
                  "text":"1234"
               },
               {
                  "type":"home",
                  "text":"company"
               }
            ]
         }
      ]
   },
   {
      "type":"image",
      "bitmapname":{
         "text":"image_14.bmp"
      },
      "command":{
         "type":"move",
         "text":"right"
      }
   }
]

The output is below:

enter image description here

AngularJS code:

<script type="text/ng-template" id="xml.html">
    <!-- XML is coming from controller as a JSON shown in my sample -->
    <ul>
        <li ng-repeat="item in XML">

            [[ item["type"] ]]

            <div ng-include=" 'xml.html' " 
                onload="XML = item.command">
            </div>

        </li>
    </ul>

</script>

<div ng-include=" 'xml.html' ">

As you may noticed last list item is empty because that "command" is not an Array

I am trying to create a recursive function that will go through the JSON and create arrays on one element "command": [{...}] in places where I have "command": {...}

Can anyone suggest a solution that will convert the object to array of object wherever I have length-one object?

NOTE

I have no control over JSON being sent from Django backed. Python is using xmltodict extension to convert XML to Dictionary. Whenever XML tags have no children it does not create a list. It only creates a dictionary.

3
  • 1
    Can you tell us why your JSON isn't consistently using an array for the .command properties? The best solution here would be to start off with a consistent data format and not to find workarounds for an inconsistent one. Commented Dec 18, 2014 at 18:22
  • The JSON is being created from XML. And the python extension that converts the XML into JSON does not create an array when "command " tag in the XML has no children Commented Dec 18, 2014 at 18:26
  • I've run into similar issues when using automatic XML -> JSON serialization. I think a good solution would be to use a data structure-aware approach to serializing the data, but I've added an answer with the workaround you're requesting. Commented Dec 18, 2014 at 18:55

2 Answers 2

1

This isn't a very generic approach, but should work for your requirement:

function fixCommands(values) {
    values.filter(function (v) {
        return 'command' in v;
    })
    .forEach(function (v) {
        if(v.command.constructor === Array) {
            fixCommands(v.command);
        } else {
            v.command = [v.command];
        }
    });
}

jsfiddle example

Sign up to request clarification or add additional context in comments.

1 Comment

Tnx JLRishe, I will let you know once I check this out
0

You can check the first item that is it object or no if it was an object you can do a loop on it otherwise it's in other type that you have mentioned at top.for example

if(arr[i].command.length===undefined) -> so is string part
else -> is array part

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.