Avoid repeat names in json

Asked

Viewed 143 times

1

I am looking for a form/function/method so that it is not possible to repeat the value saved in Json, for example:

I am saving the json below and do not want to save the same name in json.

[
  [
    {
      "id": "59a5c80dc75969297837c51e",
      "name": "uza",
      "password": "3648726"
    },
    {}
  ],
  [
    {
      "id": "59a5c811c75969297837c51f",
      "name": "kuza",
      "password": "3648726"
    },
    {}
  ],
  [
    {
      "id": "59a5c83ec75969297837c520",
      "name": "kuza",
      "password": "3648726"
    },
    {}
  ]
]

My code that creates the user in Json is this:

@api.route('/', methods=['POST'])
def create():

    # Pegar os dados da requisição
    user_json = request.get_json(silent=True)
    if not user_json:
        return "FAIL"

    # Criar uma entidade a partir do JSON
    user, errors = schema.load(user_json)
    if bool(errors):
        return jsonify(errors)
        user.save()
    return "SUCCESS"

Can you help me?

  • Although msg was edited by @Articuno, this code is very strange, I thought to edit but I was afraid to change the author’s intention. create() is not a function? then any code below the def create() would not have to be identado? the penultima line user.save() never will be executed, is that right? Sure it’s python? Wouldn’t be the case to edit the msg to make it clearer?

  • I’m sorry, it’s just that when I copy and I put it here everything gets fucked up of the identation and I didn’t realize it. I’m going to fix.

  • You will have to go through all the records in JSON and check if the name matches any already registered ones. Where is this JSON stored? In file? How you can get it within the function create?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.