0

My python code provide file upload and return the result as a json format. Json format is composed with some kind of format(refer to following json result).

How do I convert this json results to the iterable results like this: $.each(~ function(data) { ~~~ });

I don't mind changing the python code if json results were converted to iterable results.

My desired result is :

 1st row: "201510", "B12", "B120000"  "Y",  0.4897, 0.5102 , 1.0 
 2nd row: "201602", "B13", "B130001", "Y",  1.0,    0.0,     0.0 

Thank you

python code:

def bond_upload(result, prob, cust_data):
    '''
    :param result: numpy ndarray 
    :param prob: numpy ndarry 
    :param cust_data: pandas dataframe 
    :return: json format 
    '''
    return json.dumps({'result': result.tolist(), 
                           'prob': prob.tolist(), 
                           'cust_data': cust_data.tolist()})


#output result: 
{"cust_data": [["201510", "B12", "B120000" "Y"], ["201602", "B13", "B130001", "Y"]], "prob": [[0.4897, 0.5102], [1.0, 0.0]], "result":  [1.0, 0.0]} 

jquery code snippet:

$(function() {
$('#uploadBondfile').click(function() {
    var form_data = new FormData($('#formupload')[0]);
    $.ajax({
        type: 'POST',
        url: '/bond_upload',
        data: form_data,
        contentType: false,
        cache: false,
        processData: false,
        async: false,
        success: function(data) {
            //console.log(data);

            // '''''''''' code here ''''''''''' 
            //Need jquery loop throuth json result 


            alert('complete!');
        },
        error:function(x,e){
            alert('error');
        }
    });
});
});
Chris Joo
  • 577
  • 10
  • 24
  • 4
    First remove `async: false`. It's very bad practice to use that. If you check the console you'll see that the browser is even warning you not to use it. Secondly, if you `console.log(data.cust_data)` you'll see that it's a standard multi-dimensional array. You can loop through it as you would any other array – Rory McCrossan Apr 24 '17 at 07:11
  • This is probably what you are looking for http://stackoverflow.com/questions/4603574/jquery-looping-through-object-properly – Syed Abdur Rehman Kazmi Apr 24 '17 at 07:20
  • Thank you soooooo much... but It's some different due to data format unfortunately. If I follow the instruction, the result data will be displayed only single row. I added my desired output result to the Question. – Chris Joo Apr 24 '17 at 07:58

0 Answers0