1

Can I know that how can i convert my current json file to nested json structure as per below, I've tried in a couple of ways(How to convert form input fields to nested json structure using jquery), but I'm not getting it, please help me where I am doing wrong, so that we can iterate through imageurl value on my view page using ng-repeat. If I use the below, it is not giving as per my desired structure.

html:

<form id="controlid" method="post" action="" enctype="multipart/form-data" >
    <input type="hidden" value="3d{{$index}}" name="3d" >
    <input type="hidden" value="sliceX{{$index}}" name="sliceX">
    <input type="hidden" value="sliceY{{$index}}" name="sliceY" >
    <input type="hidden" value="sliceZ{{$index}}" name="sliceZ">
    <input type="button" id="submitButton" value="Save">                            
</form> 

jquery:

$("#submitButton").on('click', function(e) {
    var el = document.querySelectorAll('#text [type="hidden"]');
    var arr = [];
    var obj = {};
    var push = false;
    for(var i = 0; i < el.length; i ++){
        if(el[i].value.indexOf('/tests/images') > -1){
            if(push){
                arr.push(obj);
                obj = {};
            };
            obj.name = 'imageurl';
            obj.value = el[i].value;
            push = true;
        }
        obj.parts = obj.parts || [];
        obj.parts.push({
           name: el[i].name,
           value: el[i].value
        });
    }

    var fileDataToSave = $(obj).serializeArray();
    var jsoncontent = JSON.stringify( fileDataToSave );
    var $downloadAnchor = $("<a/>", {
        href: 'data:text/json;charset=UTF-8,' + jsoncontent,
        download: "info.json"
    });
    $downloadAnchor.css({"position": "absolute", "left": "970px", "top":"258px"});
    $downloadAnchor.text("Click here to download JSON");
    $("form").append($downloadAnchor);
    e.preventDefault();
    return false;
});

current json:

[{"name":"imageurl","value":"/tests/images/Image1.nii"},{"name":"3d","value":"3d0"},{"name":"sliceX","value":"sliceX0"},{"name":"sliceY","value":"sliceY0"},{"name":"sliceZ","value":"sliceZ0"},{"name":"imageurl","value":"/tests/images/Image2.nii"},{"name":"3d","value":"3d1"},{"name":"sliceX","value":"sliceX1"},{"name":"sliceY","value":"sliceY1"},{"name":"sliceZ","value":"sliceZ1"}]`

`desired json structure:` [
    {"name":"imageurl","value":"/tests/Image1.nii", parts: [
        {"name":"3d","value":"3d0"},
        {"name":"sliceX","value":"sliceX0"},
        {"name":"sliceY","value":"sliceY0"},    
        {"name":"sliceZ","value":"sliceZ0"},
    ]},

    {"name":"imageurl","value":"/tests/Image2.nii", parts: [
        {"name":"3d","value":"3d1"},
        {"name":"sliceX","value":"sliceX1"},
        {"name":"sliceY","value":"sliceY1"},
        {"name":"sliceZ","value":"sliceZ1"}
    ]}
]
2

1 Answer 1

1

Pure Javascript solution.

var json; // your current json object
var newJson = [];

for (var i = 0; i < json.length; i++) {
  var node;
  if (json[i].name === "imageurl") {
     node = json[i];
     var parts = [];
     for (var j = i + 1; j < json.length; j++) {
       if (json[j].name !== "imageurl") {
         parts.push(json[j]);
       } else {
         i = j - 1;
         break;
       }
     }
     node.parts = parts;
     newJson.push(node);
  }
}

we simply loop through all elements, find those with name === "imageurl" and add rest of the elements as parts until find new element with name === "imageurl".

http://plnkr.co/edit/Y7UtcwHwwo8dfP8ONk9S?p=preview

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

1 Comment

Hi teamnorge, Thanks for your reply. Yes, It worked for me. Thanks a lot !!.

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.