I am not a huge fan of either.
I think you should do something along the lines of the code below... This might not work 100% for what you are doing, but it's very important to mention.
When using JSON or an array or object, you should try to keep the format as consistent as possible. For example, if you have multiple services (twitter, reddit, etc) then you should format it so they all follow the same structure... So they all have name, client_id, etc - in the same place. In the example below, we use the data object with the subreddits array so that our code can use some special logic later on for reddit specifically.
However, even this affects the consistency a bit. All of a sudden the Twitter information is different than the Reddit information because of the special subreddits array. The reason I like to do things this way though is because you can basically say...
foreach services as service - or use some sort of loop to go through each service. Your loop won't care if you change the name of a service, so you do not need to reference it by name like services['reddit'] or services.reddit.
You can instead just say services[0] or something similar to access the first service.
The problem with your examples in my opinion is that the data is just wildly different for each. You have two services (twitter, reddit) and then a 'people' object at the bottom. It's unclear if this JSON is ideal because I don't know your exact use case, but generally speaking each JSON object should represent one data set in my opinion, or be structured properly so that names of services/things can change without affecting how they are referenced in the code.
If your code is pretty minimal & using all aspects of the JSON without looping and dynamically automating the process, you're probably fine, but for the future try to make sure that all of your data can be recursively used by your code even with changes in the future or more services being added.
JSON:
"services": [
{
"name": "reddit",
"client_id": "",
"base_url": "https://reddit.com/",
"data": [
"r/sports",
"r/news"
]
},
{
"name": "twitter",
"client_id": "",
"base_url": "https://twitter.com/",
"data": {
"wuburdev",
"accountname"
}
}
]
}
So, let's pretend we want to loop over this list. Because I do not use Python I will use PHP for this, but it will be the same for python just with different syntax/language used... the CONCEPT is the same, so I hope this helps you better understand
🙂
PHP:
$services = json_decode($json, true); // ~ line 1
$numServices = count($services); // ~ line 2
for($loop = 0; $loop < $numServices; $loop++){ // ~ line 3
$data = file_get_contents($services[$loop]["base_url"]); // ~ line 4
echo "<h4>{$services[$loop]["name"]}</h4>"; // ~ line 5
echo $data; // ~ line 6
if(count($services[$loop]["data"]) > 0){ // ~ line 7
foreach($services[$loop]["data"] as $extraData){ // ~ line 8
$newData = file_get_contents($services[$loop]["base_url"] . $extraData); // ~ line 9
echo "<hr><u>$extraData:</u><br />$newData"; // ~ line 10
} // ~ line 11
} // ~ line 12
echo "<hr>"; // ~ line 13
} // line 14
echo "<br>looping is done"; // ~ line 15
In this code it does the following:
- Line 1 takes a JSON object & turns it into a PHP array
- Count the number of services in our array $services
- Start a loop so we can go through each service the same way
- $data is our variable to get the file contents of the service URL (base_url... ex: https://reddit.com). Normally our base_url would have something like "%API_KEY%" inside of it so that every service URL can have the "client_id" or "api_key" value added to the base_url dynamically
- Echo out (in h4 tags) the service name
- Line echos out the data from step 4. We wouldn't normally do this, but this is just an example
- Check to see if this service has any data: inside of it (ex: subreddits)
- If there's extra data targets, start new loop on line 8
- Get the new file content... ex, in loop 1 we get the HTML from https://reddit.com, and then down on line 9 it'sactually going for the first data (first subreddit), so it's grabbing the HTML from: https://reddit.com/r/sports because that's our first data string, followed by r/news
- On line 10 we just echo out a line break, underlined data name, and then the new data
- On line 11 we close our little extra loop
- Line 12 we close our if statement to see if service has extra target URLs to search (ex: on twitter loop through, or the 2nd loop, we would first grab html from https://twitter.com, followed by grabbing the html for profile https://twitter.com/wuburdev )
- On line 13 we add a new line break
- on line 14 we completely stop looping the services
- On line 15 we echo out that looping is done
This example will work for an initial URL (ex: twitter.com) and any "sub page" (ex: subreddit, twitter profile, etc) that you want to search.
You could obviously change up the data or base URL to do a lot of things.... use the .JSON url on reddit to grab json data from reddit instead of raw html... or even add CodeForum into your json object to crawl CF pages!
JSON:
"services": [
{
"name": "reddit",
"client_id": "",
"base_url": "https://reddit.com/",
"data": [
"r/sports.json",
"r/news.json"
]
},
{
"name": "CF",
"client_id": "",
"base_url": "https://codeforum.org/",
"data": {
"pages/Q-A/",
"forums/Graphics/"
}
}
]
}