JSON Basics
In order to communicate to the framework what you want, you need to be able to write a language that the computer can understand. A common language used for communicating between computers and people is JSON. It has the advantage of being structured enough to be readable by a computer but can still be read and understood by people.
Below is a simple example of JSON, not related in anyway to Stardew Valley. Hopefully you are able to understand what it is communicating even without context.
{
"Name": "Jill",
"Spouse": "Jack",
"Desired Children": 3
}
While all of the symbols may not mean anything to you I hope you are able to deduce that the person this document refers to is named Jill, she's married to Jack and she wants three children. This ability to be both human readable and computer readable is what makes JSON an effective medium.
To fully understand the document, however, it is helpful to understand what the symbols in the document mean. Before we can do that we need to discuss a couple fundamental concepts, strings and numbers.
- String: Stores text characters like "A" or "2".
- Number: Stores the conceptual idea of a number.
Computers do not store the concept of the number two the same way they store the text symbol "2". When talking to a computer, you place text within quotes. Numbers are simply written as numerals, e.g. 2 or 4. In the example above the names of things are put in quotes but the number of children that Jill wants is left as the number. There are reasons for this that are out of scope for this document, but if you're interested I would encourage looking into how computers store information. For our purposes here we just need to accept that text to display and the concept of numbers are different.
{
"Name": "Jill",
"Spouse": "Jack",
"Desired Children": "3"
}
Hopefully you notice the difference in the above file, the number 3 is now being stored as a string rather than as the number. In many cases you might want to store numbers in this way, as computers can convert back and forth. In Midnight Stardew storing numbers as text is usually how its done. This is to allow for special characters to be added to the number to help the computer understand what is meant.
{
"Name": "Jill",
"Spouse": "Jack",
"Desired Children": "1-3"
}
By formatting numbers as text we can either say that Jill wants 3 children or she wants 1 to 3 children. We can't express 1 to 3 children as a number directly so we must store it as text and have the computer convert that to mean greater than or equal to 1 and less than or equal to 3.
So for our purposes you will need to put quotes around every piece of data for Midnight Stardew. This tells the computer that the data is stored as a string and doesn't have any special meaning beyond the characters between the quotes.
Now that we've talked about what the quotes mean lets discuss the three ways data can be related to each other.
- Single Value
- List
- Dictionary
Single value is what was used above. The name is Jill, spouse is Jack and the desired children is 1 to 3. Each name refers to one, and only one thing.
Lists are also pretty simple to understand, for example:
{
"Name": "Jill",
"Spouse": "Jack",
"Children":
[
"Katie",
"Julie",
"David"
]
}
Instead of looking at how many children Jill wants we created a list of children that she has, in this example Katie, Julie, and David. To let the computer know that you are creating a list you put the items in the list between square brackets [] and separate each item by a comma.
It is important to note that white space (spaces, tabs, and new lines) don't impact how the computer reads the file. So the below structure means the same thing as the structure above.
{
"Name": "Jill",
"Spouse": "Jack",
"Children": [ "Katie", "Julie", "David" ]
}
How you format your JSON files is up to you, but I strongly encourage you to prioritize your ability to read the file.
The last way to store data is in a dictionary. A dictionary stores values as a key value pair. To find a particular value you look for it in the dictionary with the key. In fact the JSON we're looking at already is a dictionary.
- The key "Name" points to the value "Jill"
- The key "Spouse" points to the value "Jack"
- The key "Children" points to the list [ "Katie", "Julie", "David" ]
The curly braces {} around all of the data, in the above example, indicate that all of the information within the file is, in fact, a dictionary. The key is provided, followed by a colon :, then the value is given. As with lists, a comma is added between each entry in the dictionary.
{
"Name": "Jill",
"Skills":
{
"Going Up Hill": "10",
"Fetching Water": "5",
"Going Down Hill": "1"
}
}
In the above example you can see that Jill has three skills and each has an associated value. You can combine single values, lists, and dictioanries to any abritrary depth, which means you can have a single value point to a dictionary, as in the above example. You can have a list of dictionaries. You can also have lists in dictionaries.
Another feature of JSON is the ability to add comments. These can be notes to yourself or to others. Something to help understand what the JSON file is doing or why you made a certain decision.
{
"Name": "Jill", // The name of the character
"Skills": // The skills that Jill has
{
// She's really good at going up a hill.
"Going Up Hill": "10",
"Fetching Water": "5",
// She's not good at going down a hill.
"Going Down Hill": "1"
}
}
The last two JSON examples will be read the same by the computer. When the computer sees two forward slashes back to back // it knows to ignore everything from there till the end of the line. Many text editors will color the lines differently to indicate that they won't be read.
If you see // in a document and it's not clarifying anything, simply delete it and everything after it to the end of the line.
Within a dictionary, the order that the key: value pairs are written also doesn't matter. So the following file would also have the same meaning to the computer.
{
"Skills":
{
"Fetching Water": "5",
"Going Down Hill": "1",
"Going Up Hill": "10"
},
"Name": "Jill"
}
In most cases it is encouraged to maintain a consistent style and order for your information. This will help you understand what you've done in the past.
With a basic understanding of the concepts discussed here you can build very complex data sets piece by piece. We'll build your confidence as we discuss the details of crafting JSON files for Midnight Stardew from very basic parts.