K
K
Kirill2020-05-16 20:07:51
Python
Kirill, 2020-05-16 20:07:51

How to translate JSON into a normal Python array?

On my site, an array is formed with a work schedule through a JS + JQuery script:

var graph = [{"start":"","end":"","lunch_start":"","lunch_end":""},
                         {"start":"","end":"","lunch_start":"","lunch_end":""},
                         {"start":"","end":"","lunch_start":"","lunch_end":""},
                         {"start":"","end":"","lunch_start":"","lunch_end":""},
                         {"start":"","end":"","lunch_start":"","lunch_end":""},
                         {"start":"","end":"","lunch_start":"","lunch_end":""},
                         {"start":"","end":"","lunch_start":"","lunch_end":""}];

            for(var i = 0; i < 7; i++) {
                graph[i]["start"] = $('[id=timepick_start][day = '+i+']').val();
                graph[i]["end"] = $('[id=timepick_end][day = '+i+']').val();
                if ($('#is_launch[day = '+i+']').is(':checked')) {
                    graph[i]["lunch_start"] = $('[id=lunch_start][day = '+i+']').val();
                    graph[i]["lunch_end"] = $('[id=lunch_end][day = '+i+']').val();
                }
            }

The graphs object is sent via ajax request to the server.
The following comes up:
<QueryDict: {'json_worktime[0][start]': [''], 'json_worktime[0][end]': [''], 'json_worktime[0][lunch_start]': [''], 'json_worktime[0][lunch_end]': [''], 'json_worktime[1][start]': ['01:00'], 'json_worktime[1][end]': [''], 'json_worktime[1][lunch_start]': [''], 'json_worktime[1][lunch_end]': [''], 'json_worktime[2][start]': [''], 'json_worktime[2][end]': [''], 'json_worktime[2][lunch_start]': [''], 'json_worktime[2][lunch_end]': [''], 'json_worktime[3][start]': [''], 'json_worktime[3][end]': [''], 'json_worktime[3][lunch_start]': [''], 'json_worktime[3][lunch_end]': [''], 'json_worktime[4][start]': [''], 'json_worktime[4][end]': [''], 'json_worktime[4][lunch_start]': [''], 'json_worktime[4][lunch_end]': [''], 'json_worktime[5][start]': [''], 'json_worktime[5][end]': [''], 'json_worktime[5][lunch_start]': [''], 'json_worktime[5][lunch_end]': [''], 'json_worktime[6][start]': [''], 'json_worktime[6][end]': [''], 'json_worktime[6][lunch_start]': [''], 'json_worktime[6][lunch_end]': [''], 'csrfmiddlewaretoken': ['gRaf2jfymU6AtnaYEq3UfurKRmfHD4MPNW6aap9zj3aYfNqkz72cjZsU6bilg6yb']}>

Next, I took it all out of QueryDict:
json.dumps(request.POST)
Now we have it all without QueryDict, but in an inconvenient form.
Is it possible to pass or convert this to its original form, which was an empty array template in the JS code?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AveWycc, 2020-05-16
@AveWycc

Try:
json.dumps(request.POST, sort_keys=True, indent=4)
https://docs.python.org/3/library/json.html

M
MechanicZelenyy, 2020-05-17
@MechanicZelenyy

Loop through the desired dictionary keys and put the values ​​into a list.
Better yet, do it normally in the initial json.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question