Click here to Skip to main content
15,918,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to retrieve only the first 2 data from a specific REST endpoint instead of all the data in the HTTP?

My input:
import requests
import json

url = "https://ws1.chic.ulster.ac.uk/SensorCentral/REST/SensorDataRangeNanos/flgSh9oHnutzvZVOnnixFNjXIda2zula_101_101?startTs=1594297110372000000&endTs=1594297110372000000"

response = requests.get(url)
data = response.text
parsed = json.loads(data)

print(json.dumps(parsed, indent=1))



where my output (which listed all the elements):

[
 {
  "sensorClass": 101, 
  "uID": "flgSh9oHnutzvZVOnnixFNjXIda2zula_101_101", 
  "eventCode": 101, 
  "sensorUUID": "flgSh9oHnutzvZVOnnixFNjXIda2zula", 
  "timeStamp": 1.594297110372e+18, 
  "deviceMfg": 101, 
  "blobJson": "[{'deviceTimestamp': 166150, 'x': 3.8919, 'y': -8.6865, 'z': -3.3077}, {'deviceTimestamp': 166170, 'x': 4.2998, 'y': -9.8624, 'z': -2.0769}, {'deviceTimestamp': 166190, 'x': 0.74341, 'y': -9.4483, 'z': 0.57692}, {'deviceTimestamp': 166210, 'x': 0.73178, 'y': -9.5004, 'z': 3.0769}, {'deviceTimestamp': 166230, 'x': 3.5538, 'y': -9.7006, 'z': 3.3077}, {'deviceTimestamp': 166250, 'x': -1.4629, 'y': -7.0692, 'z': 2.2308}, {'deviceTimestamp': 166260, 'x': -6.7571, 'y': -5.0299, 'z': -0.076923}, {'deviceTimestamp': 166280, 'x': -5.0319, 'y': -6.5896, 'z': -0.076923}, {'deviceTimestamp': 166300, 'x': -0.21122, 'y': -8.9002, 'z': 2.5}, {'deviceTimestamp': 166320, 'x': 6.3477, 'y': -9.3142, 'z': 4.5385}, {'deviceTimestamp': 166340, 'x': 10.904, 'y': -9.9498, 'z': 5.6538}, {'deviceTimestamp': 166360, 'x': 9.5501, ............................................(ALL THE DATA)


However, I want to get the out which is only contains 2 records within the blobJson string.

I want to get output as:

[
 {
  "sensorClass": 101, 
  "uID": "flgSh9oHnutzvZVOnnixFNjXIda2zula_101_101", 
  "eventCode": 101, 
  "sensorUUID": "flgSh9oHnutzvZVOnnixFNjXIda2zula", 
  "timeStamp": 1.594297110372e+18, 
  "deviceMfg": 101, 
  "blobJson": "[{'deviceTimestamp': 166150, 'x': 3.8919, 'y': -8.6865, 'z': -3.3077}, {'deviceTimestamp': 166170, 'x': 4.2998, 'y': -9.8624, 'z': -2.0769}]"
 }
]



which only 2 element in blobJson


What I have tried:

import requests
import json

url = "https://ws1.chic.ulster.ac.uk/SensorCentral/REST/SensorDataRangeNanos/flgSh9oHnutzvZVOnnixFNjXIda2zula_101_101?startTs=1594297110372000000&endTs=1594297110372000000"

response = requests.get(url)
data = response.text
parsed = json.loads(data)

print(json.dumps(parsed, indent=1))
Posted
Updated 28-Jul-20 3:25am
v3
Comments
Richard MacCutchan 17-Jul-20 8:46am    
You don't get a choice, you get whatever the server sends based on your query.
Member 14891535 17-Jul-20 9:32am    
But can I set which one to printout?
Richard MacCutchan 17-Jul-20 9:39am    
Yes, but you need to write the code to extract the part that you want.
Member 14891535 17-Jul-20 9:40am    
Any idea can advice? which I struggle in
Richard MacCutchan 17-Jul-20 9:46am    
Yes, go to json — JSON encoder and decoder — Python 3.8.4 documentation[^] and you will find all the information you need on handling JSON.

1 solution

Do as follows.


Python
import requests
import json

url = "https://ws1.chic.ulster.ac.uk/SensorCentral/REST/SensorDataRangeNanos/flgSh9oHnutzvZVOnnixFNjXIda2zula_101_101?startTs=1594297110372000000&endTs=1594297110372000000"

response = requests.get(url)
data = response.text
parsed = json.loads(data)
# new
# Get only the 2 first "blobJson" elements
blobJsonData = (parsed[0]["blobJson"])
json_formated_data = blobJsonData.replace("'", "\"")
json_data = json.loads(json_formated_data)
json_data_filtered = json_data[0:2]

# Update the data struct
parsed[0]["blobJson"] = json.dumps(json_data_filtered).replace("\"", "'")
# new

print(json.dumps(parsed, indent=1))
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900