Need help JSON, Markdown, & Python

Seneca

New Coder
Apr 15, 2019
18
9
3
Australia
myanimelist.net
What looks cleaner?
JSON:
{
  "Reddit": {
    "client_id": "",
    "client_secret": "",
    "user_agent": "",
    "username": "",
    "password": "",
    "Subreddits": [
      "", ""
    ]
  },
  "Twitter": {
    "consumer_key": "",
    "consumer_secret": "",
    "access_token": "",
    "access_token_secret": "",
     "base_url": "https://twitter.com/",
      "mobile_url": "https://mobile.twitter.com/"
    },
    "Monitor": [
      "", ""
    ]
  }
}
VS:
JSON:
{
  "Reddit": {
    "client_id": "",
    "client_secret": "",
    "user_agent": "",
    "username": "",
    "password": "",
    "Subreddits": [
      "", ""
    ]
  },
  "Twitter": {
    "keys": {
      "consumer_key": "",
      "consumer_secret": ""
    },
    "access": {
      "access_token": "",
      "access_token_secret": ""
    },
    "URLs": {
      "base_url": "https://twitter.com/",
      "mobile_url": "https://mobile.twitter.com/"
    }
  },
  "People": [
    {
      "name": "",
      "instagram": "",
      "youtube": "",
      "twitter": ""
    }
  ]
}
 
Last edited:

Seneca

New Coder
Apr 15, 2019
18
9
3
Australia
myanimelist.net
1) Is it possible to do some sort of phrase system with json?
2) Will markdown work through json? (ex: { "faq": "[text](text link)
3) Can I have multiple user credentials for posting content? (EG: User 1 only posts content from user_1_twitter but user 2 post content from 3,4,5,6)
 
Last edited:

Ghost

Active Coder
Moderator
Apr 19, 2019
234
94
28
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:
  1. Line 1 takes a JSON object & turns it into a PHP array
  2. Count the number of services in our array $services
  3. Start a loop so we can go through each service the same way
  4. $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
  5. Echo out (in h4 tags) the service name
  6. Line echos out the data from step 4. We wouldn't normally do this, but this is just an example
  7. Check to see if this service has any data: inside of it (ex: subreddits)
  8. If there's extra data targets, start new loop on line 8
  9. 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
  10. On line 10 we just echo out a line break, underlined data name, and then the new data
  11. On line 11 we close our little extra loop
  12. 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 )
  13. On line 13 we add a new line break
  14. on line 14 we completely stop looping the services
  15. 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/"
            }
        }
    ]
}
 
Last edited:

Ghost

Active Coder
Moderator
Apr 19, 2019
234
94
28
1) Is it possible to do some sort of phrase system with json?
2) Will markdown work through json? (ex: { "faq": "[text](text link)
3) Can I have multiple user credentials for posting content? (EG: User 1 only posts content from user_1_twitter but user 2 post content from 3,4,5,6)
1) What do you mean "phrase system" ?

2) Well, JSON is usually referenced like data['key']... for data = {key: "value"}
You could do something like this....
JSON:
{
    "faq":"https://google.com",
    "other":"https://codeforum.org"
}
With Python...
Python:
links = '{"faq":"https://google.com","other":"https://codeforum.org"}'
# the links JSON would come from something else obviously, like retreiving from API or a .json file
import json
usejson = json.loads(links)
print(usejson["faq"])
print("The other link is " + usejson["other"])

3) Yes, you could do that. You would need to set up some sort of relationship data...
So you could have user1 attached to an array of twitters, like so (this is just Python now, not json below):
Python:
users = {
    "user1":[0],
    "user2":[1,2,3,4,5]
}
twitters = [
    "content0",
    "content1",
    "content2",
    "content3",
    "content4",
    "content5",
]
for user, contents in users.items(): # loop each user-content in users
    print(user + " Content:") # display which user we're on
    for content in contents: # loop through the content IDs in users data
        print(twitters[content]) # grab the content from the twitters data
        
# replace prints with actual code to do stuff with it :)