I believe your JSON would look something like:
var Types = {
NormalHR: {
Reinforcement: 0,
Weight: 100,
Message: 'Great! Your heart rate is in the zone.',
EnforcementSound: 'ding'
},
HighHR: {
Reinforcement: 1,
Weight: 100,
Message: 'Slow down. Your heart rate is too high!'
},
LowHR: {
Reinforcement: -1,
Weight: 100,
Message: 'Speed up. Low heart rate.'
}
};
As @Balder says in their answer, you can then access use dictionary-style syntax, like:
Types['NormalHR']['Reinforcement']
You could also use property-accessor syntax, like:
Types.NormalHR.Reinforcement
The reason I didn't include the "type" of each item, is that you can easily infer it for building your grid - as follows:
typeof Types.NormalHR.Reinforcement(this will return"number")typeof Types.NormalHR.Message(this will return"string")
Similarly, to get the counts - you can count the properties of a specific object. In modern browsers, try:
Object.keys(Types.NormalHR).length(this will return2)
For older browsers, refer to other methods here: How to efficiently count the number of keys/properties of an object in JavaScript?
Hope this helps!
Answer from Troy Alford on Stack OverflowJSON / Dictionary Action Examples
JSON Effectively is a Dictionary? Change My Mind
JSON and Dictionary
Dictionary in JSON format
I believe your JSON would look something like:
var Types = {
NormalHR: {
Reinforcement: 0,
Weight: 100,
Message: 'Great! Your heart rate is in the zone.',
EnforcementSound: 'ding'
},
HighHR: {
Reinforcement: 1,
Weight: 100,
Message: 'Slow down. Your heart rate is too high!'
},
LowHR: {
Reinforcement: -1,
Weight: 100,
Message: 'Speed up. Low heart rate.'
}
};
As @Balder says in their answer, you can then access use dictionary-style syntax, like:
Types['NormalHR']['Reinforcement']
You could also use property-accessor syntax, like:
Types.NormalHR.Reinforcement
The reason I didn't include the "type" of each item, is that you can easily infer it for building your grid - as follows:
typeof Types.NormalHR.Reinforcement(this will return"number")typeof Types.NormalHR.Message(this will return"string")
Similarly, to get the counts - you can count the properties of a specific object. In modern browsers, try:
Object.keys(Types.NormalHR).length(this will return2)
For older browsers, refer to other methods here: How to efficiently count the number of keys/properties of an object in JavaScript?
Hope this helps!
You dont have "dictionaries" in json, but you have key/value arrays which works pretty much like them.
In your code if you want to access:
Normal Cadence > Message you only must do:
Types["NormalCadence"]["Message"]
Is similar to a .Net Dictionary, but is not exactly a Dictionary
Shortcuts located here: Building/Using Dictionaries:
https://www.icloud.com/shortcuts/3e5a3524dc0e434cba55549f123605b4
Building Nested Dictionary Values:
https://www.icloud.com/shortcuts/ff1b76d0e41f47cb8cdfa0754edfbc0d
I wanted to share two shortcuts that I created as sort of a memory reference for future Shortcuts that I might create that utilize Dictionary Actions. This work is based on knowledge that I gained from Shortcut Masters on Reddit like gluebyte, mvan231, and others. My thanks to them!
These Shortcuts are meant to be inspected and run in the Shortcut development mode i.e, from within the Shortcuts app, opened for editing so that the comments can be reviewed.
These Shortcuts are designed as a tutorial of sorts for beginners who may be struggling with how to use Dictionary Actions and how JSON formatted strings can be created and used.
The first Shortcut, titled โBuilding/Using Dictionariesโ builds a simple โKey/Valueโ pair Dictionary and demonstrates some methods for adding Key/Value pairs, as well as some examples of user interaction with a Dictionary object.
The second Shortcut, titled โBuilding Nested Dictionary Valuesโ demonstrates a method for creating more complex โNestedโ JSON strings by using a โTemporaryโ Dictionary Action to build partial JSON Key/Value pairs that can then be appended to โFirst Levelโ Keys in a Dictionary.
Finally, although Iโm not new to programming in general, I have only been working with Shortcuts for about a week, and fully expect a reply from some Reddit user, (perhaps one of the โMastersโ), saying, โOk, but thereโs a lot easier way to do this or thatโ, and then provide me an example. In the meantime, maybe these Shortcuts can provide some guidance to beginners that wish to utilize the very handy Dictionary Actions in Shortcuts.
Isn't JSON format effectively a Python dictionary, in that they follow the same format? Isn't that right? Thanks guys!
Once in an interview I was asked the difference between JSON and Dictionary. So I decided to write a blog post about it. Do check it out. Link
https://raw.githubusercontent.com/matthewreagan/WebstersEnglishDictionary/master/dictionary_alpha_arrays.json
I found JSON files containing dictionary data, but often the data isn't organized into arrays and objects. You want to have an object indicating the type of word, the definition and other information, but often times the JSON you see on Github are just a simple one-to-one mapping. Is there any better more informative and structured dictionary JSON files out there?