I have a question about filtering and formatting JSON data received from an API, and I'm curious how others might solve it. It's pretty simple, but I wanted to include data here so you can play with it if desired.
Here's a JSON response I got from WeatherAPI.com and I didn't find a way of filtering for including specific fields:
—————————————————————
{"body":{"current":{"cloud":75,"condition":{"code":1003,"icon":"//cdn.weatherapi.com/weather/64x64/night/116.png","text":"Partly cloudy"},"dewpoint_c":6.1,"dewpoint_f":42.9,"feelslike_c":18.3,"feelslike_f":64.9,"gust_kph":6.6,"gust_mph":4.1,"heatindex_c":19.4,"heatindex_f":66.9,"humidity":37,"is_day":0,"last_updated":"2025-02-08 23:45","last_updated_epoch":1739083500,"precip_in":0,"precip_mm":0,"pressure_in":29.99,"pressure_mb":1016,"temp_c":18.3,"temp_f":64.9,"uv":0,"vis_km":16,"vis_miles":9,"wind_degree":203,"wind_dir":"SSW","wind_kph":3.6,"wind_mph":2.2,"windchill_c":19.1,"windchill_f":66.5},"location":{"country":"USA","lat":33.5099,"localtime":"2025-02-08 23:58","localtime_epoch":1739084335,"lon":-112.08,"name":"Phoenix","region":"Arizona","tz_id":"America/Phoenix"}},"headers":{"Cache-Control":"public, max-age=180","Cdn-Cache":"EXPIRED","Cdn-Cachedat":"02/09/2025 06:59:04","Cdn-Edgestorageid":"1234","Cdn-Proxyver":"1.19","Cdn-Pullzone":"93447","Cdn-Requestcountrycode":"US","Cdn-Requestid":"0859189782e6a71b8814912e73b7627f","Cdn-Requestpullcode":"200","Cdn-Requestpullsuccess":"True","Cdn-Requesttime":"0","Cdn-Status":"200","Cdn-Uid":"8fa3a04a-75d9-4707-8056-b7b33c8ac7fe","Content-Type":"application/json","Date":"Sun, 09 Feb 2025 06:59:04 GMT","Server":"BunnyCDN-IL1-1207","Vary":"Accept-Encoding","X-Weatherapi-Qpm-Left":"4999962"},"status":200}
—————————————————————
When you prettify it, you get this:
—————————————————————
{
"body":{
"current":{
"cloud":75,
"condition":{
"code":1003,
"icon":"//cdn.weatherapi.com/weather/64x64/night/116.png",
"text":"Partly cloudy"
},
"dewpoint_c":6.1,
"dewpoint_f":42.9,
"feelslike_c":18.3,
"feelslike_f":64.9,
"gust_kph":6.6,
"gust_mph":4.1,
"heatindex_c":19.4,
"heatindex_f":66.9,
"humidity":37,
"is_day":0,
"last_updated":"2025-02-08 23:45",
"last_updated_epoch":1739083500,
"precip_in":0,
"precip_mm":0,
"pressure_in":29.99,
"pressure_mb":1016,
"temp_c":18.3,
"temp_f":64.9,
"uv":0,
"vis_km":16,
"vis_miles":9,
"wind_degree":203,
"wind_dir":"SSW",
"wind_kph":3.6,
"wind_mph":2.2,
"windchill_c":19.1,
"windchill_f":66.5
},
"location":{
"country":"USA",
"lat":33.5099,
"localtime":"2025-02-08 23:58",
"localtime_epoch":1739084335,
"lon":-112.08,
"name":"Phoenix",
"region":"Arizona",
"tz_id":"America/Phoenix"
}
},
"headers":{
"Cache-Control":"public, max-age=180",
"Cdn-Cache":"EXPIRED",
"Cdn-Cachedat":"02/09/2025 06:59:04",
"Cdn-Edgestorageid":"1234",
"Cdn-Proxyver":"1.19",
"Cdn-Pullzone":"93447",
"Cdn-Requestcountrycode":"US",
"Cdn-Requestid":"0859189782e6a71b8814912e73b7627f",
"Cdn-Requestpullcode":"200",
"Cdn-Requestpullsuccess":"True",
"Cdn-Requesttime":"0",
"Cdn-Status":"200",
"Cdn-Uid":"8fa3a04a-75d9-4707-8056-b7b33c8ac7fe",
"Content-Type":"application/json",
"Date":"Sun, 09 Feb 2025 06:59:04 GMT",
"Server":"BunnyCDN-IL1-1207",
"Vary":"Accept-Encoding",
"X-Weatherapi-Qpm-Left":"4999962"
},
"status":200
}
—————————————————————
The next thing for the user to do is select the particular items they're interested in seeing.
As an aside, in VCL I'd put this into a TMemo and set it to enable LineSelect
and MultiSelect
, but in WEB Core, the memos don't seem to offer that. So I made a TWebCheckList to select items. This thing trims off the blank space in front, so you see something that looks like this:
Now, here's what I'm curious about.
I'd like to allow the user to save these selections somewhere as a filter so when they run this query in the future, it would show only these values in a formatted display. Without any formatting you get this from the selected items:
"humidity": 39,
"last_updated": "2025-02-14 23:30",
"temp_f": 61,
"wind_dir": "W",
"wind_mph": 10.5,
"name": "Phoenix",
"region": "Arizona",
My question is ... how would you SAVE THESE SELECTIONS, so that when the query is run later, they get applied as a FILTER and it would let you display these data items in a formatted way?
Maybe something like:
Weather for: Phoenix Arizona as of: 2025-02-14 23:30
Temperature: 61
Humidity: 39
Wind Direction: W @ 10.5 MPH
It seems to me that, in general, you can't just save the key for each of the JSON pairs because they could be duplicated -- eg, maybe there's an array of items and you only want what's in one of them. So then you'd probably want to save the PATH to each item? If so, how would you do that so that it would be easy to take the list of saved paths and run them on the latest JSON string and then pass the values to another method that formats and displays the results.
Is there a library that lets you do this?
This doesn't seem like a very complex or uncommon type of problem to solve. It reminds me of what's done in XML data extraction and formatting for reports (which I've seen but have no direct experience with).