If this coding format looks almost self explanatory, well, that’s because it almost is!
Even though JSON is Java Script Native, JSON is a Data Format – Not a Script!
The first thing that pops up is called a “key”: “value” pair as they are called, the “Key” defines common items like an Email / FirstName / LastName / Etc, while the “Value” defines the unique portion like an Employee #, Name, Email, Etc.
JSON REQUIRES(!!!) your “key” value to be in double quotes, no exceptions!
There are some exceptions with the “value” where its more flexible, and Java Script will allow single quotes for key pairs, but if you are looking at a JSON File with the keys using single quotes then you are looking at an invalid JSON File!
Each one of these entries is enclosed with Curly Brackets { } which makes this a “Java Script Object” for each one of these, how all these JS Objects are wrapped inside of Regular Brackets [ ] which makes in Java Script makes it an Array of JS Objects.
The take away here being that curly brackets { } contain our Key Pairs, and our Array as denoted with Brackets [ ] contains our Java Script Objects which contains our Key Pairs.
And that is all there is too it, lesson is over, thanks for stopping by!
EXCEPT THAT IS NOT ALL THERE IS TOO IT! And actually far from all there is to it!
I can take this existing Array which is a perfectly fine piece of JSON Code, and take it a level deeper by nesting this Java Script Array INSIDE another Java Script Object!
Behold:
Being its such a tiny add I noted it with little red stars to indicate where I put the Array inside a Java Script Object with Curly Brackets { }, and because Java Script Objects contain a Key Value Pair I entered the Key on Line #1 {“LoopedBackEmployees”: and then the entire Array that contains JS Objects is the Pair Value to the Key.
So now LoopedBackEmployees = Myself and my two imaginary workers (we are going through staffing changes at my imaginary company), which logically makes absolutely perfect sense, however I would pay particular attention to how brackets line up!
Note – In the above picture the Array used as the “Pair” in this Key Pair has the [{ on the same line which is completely valid, it can also be indented to and be valid:
It does need to be indented of course along with the rest of the code ( you can highlight enter chunks of code and hit the tab button to indent code in chunks in VSC), however both of these are legit ways of writing the same script so watch for this on exam day
JSON can also be formatted in just about any way, meaning that a NULL Space or White Line (however you call it) in the script will not impact how the script runs, and it could also be on a single line of code though it would not be very easy to read (but is valid).
Now to adjust some values within the JSON file in the Key: Value pair
As mentioned the Key must must must have double quotes ” ” however the Value is more flexible, and in this example I will review how some things work and others don’t.
Interestingly we get into some Python Syntax / Values with the JSON code!
For example we do not need to place #’s or “Integers” within quotes, while we DO need to put text values like names with quotes which are called “Strings” – Sound familiar? 😀
Also we can add Boolean Values (true, false) into the Script as well shown below.
Also the colons separating the Key : Pair can have spaces between them (but I would keep it consistent of course outside of a lab / testing script).
Also you can add null lines anywhere and it will not make the script invalid.
Behold!!!:
Absolutely, 100%, valid JSON File. Ugly, but valid.
Also I fired my imaginary employees in this example, and yes they will be missed.
All joking aside, as long as those brackets line up to define where JS Objects and Arrays begins and ends, its basically the gloves can come off in terms of formatting.
Now in terms of Integers it has to be a raw integer to not be a “String” in JSON, so even if you have a date that Java Script would recognize as a number or date, JSON see’s 7/9/2020 as a string value so it must be in double quotations or a “String” value!
The Key : Value pair can have null space, there can be tons of null lines between lines of code, however there are just those few rules that are fairly basic to remember.
Also you need commas separating values except for the last value, a comma on the last value will cause the script to error out / become invalid – so watch the commas!
There is one last formatting item to discuss that I forgot from by Keith Bogart INE course on JSON (he did a really great job!), is that you can nest JS Objects inside of JS Objects.
For example if I wanted to make the name a single Key Pair with a nested Key Pair within it, I would format it as follows:
This can also be done with an Array, and make a massive script, but its been too long of a day to make an example that has multiple Key Pairs that contain Arrays nested in them 🙂
And that is all there is to JSON scripting / coding for DevNet exam and beyond!
There are some small things I did not touch on like how to call out objects by turning this into a .js (java script) file for the sake of clarity and focusing on JSON solely, however this knowledge should easily carry you through DevNet and beyond (and hopefully me!).
Until next time!