How to subscribe to a channel using Jquery to read data in your channel

3 views (last 30 days)
Hi Team
I am trying to read API key to my channel, using jquery to get JSON response to my channel. Here is my jquery logic, i am unable to get a response to my channel i have created and i want to read one field from my channel (field8) only.
<script
src="https://code.jquery.com/jquery-3.3.1.js"integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="crossorigin="anonymous"></script>
<script type="text/javascript">
function loadData(){
var id;
//get the data from thingspeak.
$.getJSON('https://api.thingspeak.com/channels/899906/fields/8.json?api_key=F35GLOFJ8L99D0OM&results=2',function(data) {
id = data.field8;
if(id){
displayData(id);
}
});
}
</script>

Answers (1)

Yousef
Yousef on 4 Sep 2023
Here is how you can read a specific field from a JSON response in MATLAB using the JSONlab toolbox:
  1. Install JSONlab toolbox if you don't already have it:
pkg install -forge jsonlab
  1. Make a GET request to your API endpoint to get the JSON response:
url = 'https://api.thingspeak.com/channels/YOUR_CHANNEL_ID/feeds.json?api_key=YOUR_API_KEY';
json_text = webread(url);
  1. Parse the JSON text into a MATLAB struct:
json = jsondecode(json_text);
  1. Access the specific field you want from the struct (field8 in this example):
field8_data = json.feeds(1).field8;
The field8_data variable will now contain the data for field8 from the first entry in the feeds array.
You can loop through all entries to access field8 for each:
for i = 1:length(json.feeds)
field8(i) = json.feeds(i).field8;
end

Communities

More Answers in the  ThingSpeak Community

Categories

Find more on Read Data from Channel in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!