R
R
rustam9ksenov2021-05-27 19:20:58
Python
rustam9ksenov, 2021-05-27 19:20:58

Generate Json object?

Need help with a seemingly simple yet complex issue. I create a json object like this. How to create it so that there is also an archive inside the key? I try the generation method but I get a string. Which json.dumps() does not accept.

pole = {
  "recordsFiltered": 2,
  "data": [
    {
      "history_money": "-11 часов",
      "history_text": "НОМЕР: 79089688399 Абонет: Доступен"
    },
    {
      "history_money": "+11 часов",
      "history_text": "НОМЕР: 79189688399 Абонет: Доступен"
    }
  ]
}
print(pole['data'][0]['history_money'])


What I want to see. Add to 'data' object: array

My implementation method.

import json
pol = {"history_money": "-11 часов", "history_text": "НОМЕР: 79089688399 Абонет: Доступен"}

rep = {"recordsFiltered":1,"data":"" + str(pol) + ""} 
print(rep)
print(rep['data'][0]['history_money'])


As a result, I get an error. or an empty array.

{'recordsFiltered': 1, 'data': []}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Kuts, 2021-05-27
@rustam9ksenov

pol = {
    "history_money": "-11 часов",
    "history_text": "НОМЕР: 79089688399 Абонет: Доступен"
}

rep = {
    "recordsFiltered": 1,
}

rep['data'] = [pol]

print(rep)
print(rep['data'][0]['history_money'])

and here is your json object
import json

print(json.dumps(rep, indent=4, ensure_ascii=False))

# {
#    "recordsFiltered": 1,
#    "data": [
#        {
#            "history_money": "-11 часов",
#            "history_text": "НОМЕР: 79089688399 Абонет: Доступен"
#        }
#    ]
#}

H
HemulGM, 2021-05-27
@HemulGM

JSON is not "generated" like that. The data inside must be escaped. You need a json serializer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question