N
N
Nodir Malikov2021-04-24 15:15:29
Python
Nodir Malikov, 2021-04-24 15:15:29

How to check JSON for updates?

I get the JSON itself through requests and save it to a file, but I only need to save the new object inside the JSON and output it to the console.

JSON example

{
  "jsonrpc": "2.0",
  "id": "fbad5793-e89e-45c4-9f3f-4b324bc82ba3",
  "result": [
    {
      "id": "60831f62a40a2eb3973c9df3",
      "account": [
        {
          "name": "transaction",
          "title": "����� ����",
          "value": "1315"
        }
      ],
      "air": "185784234",
      "cancel_time": 0,
      "cancelled_at": null,
      "check_number": 1315,
      "created_at": "2021-04-23T19:26:34.607Z",
      "currency": 860,
      "detail": null,
      "error": null,
      "is_adaptive": false,
      "is_adaptive_batch": false,
      "is_adaptive_chunk": false,
      "is_corporate": false,
      "is_refund": false,
      "is_refundable": false,
      "refund": null,
      "refund_receipts": [],
      "refunded_receipt": null,
      "rrn": "111400433419",
      "stan": "148864",
      "state": 4,
      "state_code": "paid",
      "tid": "97006627",
      "viewed": false
    },
    {
      "id": "8da31f62a40a2eb3973c9df3",
      "account": [
        {
          "name": "transaction",
          "title": "����� ����",
          "value": "1314"
        }
      ],
      "air": "185784234",
      "cancel_time": 0,
      "cancelled_at": null,
      "check_number": 1314,
      "created_at": "2021-04-23T19:26:34.607Z",
      "currency": 860,
      "detail": null,
      "error": null,
      "is_adaptive": false,
      "is_adaptive_batch": false,
      "is_adaptive_chunk": false,
      "is_corporate": false,
      "is_refund": false,
      "is_refundable": false,
      "refund": null,
      "refund_receipts": [],
      "refunded_receipt": null,
      "rrn": "111400433419",
      "stan": "148864",
      "state": 4,
      "state_code": "paid",
      "tid": "97006627",
      "viewed": false
    }
  ]
}

Each object is stored in result.
I didn't find anything good on the internet.
The API itself does not have such a method , for example, as in the Telegram Bot API - getUpdates, so you have to implement it yourself.
Is there any library? How to get updates and what is the best way to implement?

upd: Each object and each request has an ID .
The ID of each object is greater than the previous one by 1.

upd2: If you make requests too often, you can get an API ban

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
shurshur, 2021-04-24
@Fayo

upd: Each object and each request has an ID.
The ID of each object is greater than the previous one by 1.

Remember the largest of the id already returned, on each request, check if the id is greater than the remembered one. Make requests at certain intervals (sleep) so that their number per unit of time does not exceed the limits (for example, if a maximum of 10 requests per minute is allowed, then you need to make an interval of at least 6 seconds).
By the way, getUpdates in the telegram libraries are used in much the same way. Moreover, the Bot API allows you to specify the desired id in the offset parameter so that only newer ones are returned in response.

O
o5a, 2021-04-24
@o5a

It's not entirely clear what the actual question is? How to compare 2 received json results? Or how to make a request with an interval?
If the objects are unique by id, then you can get a list (set) of identifiers of one request and another, then find the differences in id and use them to display the desired results from the new request.

ids = {result['id'] for result in data['result']}
print(ids)

for json from the example it will turn out
{'8da31f62a40a2eb3973c9df3', '60831f62a40a2eb3973c9df3'}

Let's say the new json came with additional. id (new_ids)
{'8da31f62a40a2eb3973c9df3', '60831f62a40a2eb3973c9df3', '60831f62a40a2eb3973c9df5'}

Accordingly, we get a list of new id:
diff_ids = new_ids - ids
And we already display data on them:
news = [result for result in new_data['result'] if result['id'] in diff_ids]

where new_data is the new json request

Y
Yupiter7575, 2021-04-24
@yupiter7575

while True:
    sleep(60)
    pass

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question