Home > other >  for loop iteration in python for new value inside a nested json
for loop iteration in python for new value inside a nested json

Time:11-23

This is my first post, trying the get the hang of python to setup an api backend to mongodb (through mongoengine).

I have this part of the code:

    def post(self) -> Response:
        data = request.get_json()
        post_list = Lists(**data).save()
        output = {'id': str(post_list.id)}
        return jsonify({'result': output})

In which I receive a json from the front-end, stored to the database, which all works fine. This is the raw json:

{
   "listTitle":"hjhk",
   "listId":"873",
   "listCreator":"lotte",
   "sublist":[
      {
         "subListCreator":"lotte",
         "item":[
            {
               "itemTitle":"njkn",
               "itemContext":"None",
            },
            {
               "itemTitle":"njk",
               "itemContext":"None"
            },
            {
               "itemTitle":"vbvb",
               "itemContext":"None"
            }
         ]
      }
   ]
}

However, I'm trying to add an id field to the lowest arrays in the structure to indicate the position of the item in the sublist (1, 2, 3, etc.). For this I was searching for an appropriate for loop. Tried many things, this is the furthest I came so far:

    def post(self) -> Response:
        data = request.get_json()
        for sublist in data['sublist']:
            for item in sublist['item']:
                if 'positionId' not in item:
                    item['positionId'] = 1
            item['positionId']  = 1
        post_list = Lists(**data).save()
        output = {'id': str(post_list.id)}
        return jsonify({'result': output})

Resulting in:

{
   "listTitle":"dasdsa",
   "listId":"873",
   "listCreator":"lotte",
   "sublist":[
      {
         "subListCreator":"lotte",
         "item":[
            {
               "itemTitle":"dasd",
               "itemContext":"None",
               "positionId":1
            },
            {
               "itemTitle":"dsad",
               "itemContext":"None",
               "positionId":1
            },
            {
               "itemTitle":"dsadsa",
               "itemContext":"None",
               "positionId":2
            }
         ]
      }
   ]
}

I know I'm doing stuff wrong, I googled also quite a lot. Found a lot of related stuff, but nothing made sense to me. Hope somebody is able to help me out :)

In addition, I also would like to add an automatic scorefield with a similar system as the positionId one. Only here the first value would be 3, the next 2, and everything that follows later needs to have a score of 1.

Well I hope the answer is easy, thanks a lot already considering the question!!

CodePudding user response:

The enumerate() function in python should be of use to you:

    def post(self) -> Response:
    data = request.get_json()
    for sublist in data['sublist']:
        for i,item in enumerate(sublist['item']):
            item['positionId'] = i
    post_list = Lists(**data).save()
    output = {'id': str(post_list.id)}
    return jsonify({'result': output})
  • Related