Detect change in JSON [closed]

2

I put you in situation, I have an API that returns the JSON (for example link ) to which I can only access to obtain the data, I can not touch your code.

I would like to know every time there is a change in the JSON (for example temp, pressure, temp_max) to do something in programming, for example, write in a txt.

Normally I use PHP, but I can not find anything to do this, any suggestion is welcome whatever the language, as long as it is web oriented, to show the results.

    
asked by alebupal 14.05.2018 в 08:47
source

1 answer

1

For python there is a library called jsondiff that can be useful, along with the library requests

In the following example we downloaded the JSON every 10 minutes, and I applied diff between what we just downloaded and what we had downloaded in the previous iteration [Note: in free plan , the OpenWeather api updates your data every 2h , so you can increase the sampling time without problems, because this example would not produce results until have executed about 12 times the loop).

import time    
import requests
from jsondiff import diff


api_url = 'http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22'
data = requests.get(api_url).json()

while (True):
  time.sleep(10*60)
  newdata = requests.get(api_url).json()
  diferencia = diff(newdata, data)
  if diferencia:
    print(diferencia)
  data = newdata

In this example I limit myself to save the result of the diff (which will be an empty dictionary in case there have been no changes), and then print it if there were changes. But you can do anything else from that result. Look at the documentation of jsondiff to see what structure the json has that returns diff when there were changes.

    
answered by 14.05.2018 в 09:19