So why don't you simply use a key-value literal?
var params = {
'slide0001.html': 'Looking Ahead',
'slide0002.html': 'Forecase',
...
};
return params['slide0001.html']; // returns: Looking Ahead
Answer from Crozin on Stack OverflowSo why don't you simply use a key-value literal?
var params = {
'slide0001.html': 'Looking Ahead',
'slide0002.html': 'Forecase',
...
};
return params['slide0001.html']; // returns: Looking Ahead
If the logic parsing this knows that {"key": "slide0001.html", "value": "Looking Ahead"} is a key/value pair, then you could transform it in an array and hold a few constants specifying which index maps to which key.
For example:
var data = ["slide0001.html", "Looking Ahead"];
var C_KEY = 0;
var C_VALUE = 1;
var value = data[C_VALUE];
So, now, your data can be:
[
["slide0001.html", "Looking Ahead"],
["slide0008.html", "Forecast"],
["slide0021.html", "Summary"]
]
If your parsing logic doesn't know ahead of time about the structure of the data, you can add some metadata to describe it. For example:
{ meta: { keys: [ "key", "value" ] },
data: [
["slide0001.html", "Looking Ahead"],
["slide0008.html", "Forecast"],
["slide0021.html", "Summary"]
]
}
... which would then be handled by the parser.
Whether you choose the first or the third option depends on your use case. If you are modeling many different instances of the same type of thing, choose the first. For example, you have a list of people. If you are modeling many different attributes of one thing, choose the third. You can have repeated keys in the first format, but not in the third.
The second option is terrible, and I've yet to find an appropriate use case for it. The reason it's terrible, in addition to being more verbose, is that for single-level JSON, it breaks most libraries' automatic conversion to a dictionary/map. For deeply-nested JSON, it breaks the XPath-like query interface.
This makes it a pain to work with. And if you don't know your keys at compile time, you will want a dictionary or XPath interface, because you won't be able to convert it to a class. It may not seem like a big deal now, but the longer you have a data format, the harder it will be to change.
You say these are key / value pairs. In that case, use #3: dictionary of key / value pairs.
If these are not key / value pairs, then don't call them "keys" and "values" and use #2, an array of dictionaries with arbitrary contents.
Structure #1 is just daft unless you need key / value pairs but also their order. Which you rarely do.
How do I process the key-value pairs in an array of JSON objects?
How to make data in an array the keys of a JSON file?
JSON manipulation from key-value to array?
What is the method to insert a value in a JSON object array?
Videos
Hi, I'm new to powershell (just started yesterday).
I am trying to make a script that gets the disk info in a JSON format. So far I have
$disk_list = @([System.IO.DriveInfo]::GetDrives() | ConvertTo-Json -Depth 1)
This returns:
[
{
"Name": "C:\\",
"DriveType": 3,
"DriveFormat": "NTFS",
"IsReady": true,
"AvailableFreeSpace": 141158105088,
"TotalFreeSpace": 141158105088,
"TotalSize": 171220922368,
"RootDirectory": "C:\\",
"VolumeLabel": ""
},
{
"Name": "D:\\",
"DriveType": 5,
"DriveFormat": null,
"IsReady": false,
"AvailableFreeSpace": null,
"TotalFreeSpace": null,
"TotalSize": null,
"RootDirectory": "D:\\",
"VolumeLabel": null
}
]Now I want to be able to go through each object in the array, and then through each key-value pair, and alter the key slightly.
I have tried several foreach loops, but they don't seem to be working. Even something as simple as:
ForEach ($obj in $disk_list) {
echo $obj
echo "*********************************"
}is not doing what I expected it to. It just prints the whole $disk_list array with one line of "**********************" at the end, instead of printing each object followed by a "**********************".
My end goal is to achieve something along the lines of (in pseudocode):
ForEach ($obj in $disk_list) {
ForEach ($kv_pair in $obj) {
$key = lowercase($kv_pair.Key)
kv_pair.Key = "{#" + $key + "}"
# So if the key used to be "Name", it should be transformed to "{#Name}"
}
}How can I achieve this in powershell?
A "JSON object" is actually an oxymoron. JSON is a text format describing an object, not an actual object, so data can either be in the form of JSON, or deserialised into an object.
The JSON for that would look like this:
{"KEY1":{"NAME":"XXXXXX","VALUE":100},"KEY2":{"NAME":"YYYYYYY","VALUE":200},"KEY3":{"NAME":"ZZZZZZZ","VALUE":500}}
Once you have parsed the JSON into a Javascript object (called data in the code below), you can for example access the object for KEY2 and it's properties like this:
var obj = data.KEY2;
alert(obj.NAME);
alert(obj.VALUE);
If you have the key as a string, you can use index notation:
var key = 'KEY3';
var obj = data[key];
var object = {
key1 : {
name : 'xxxxxx',
value : '100.0'
},
key2 : {
name : 'yyyyyyy',
value : '200.0'
},
key3 : {
name : 'zzzzzz',
value : '500.0'
},
}
If thats how your object looks and you want to loop each name and value then I would try and do something like.
$.each(object,function(key,innerjson){
/*
key would be key1,key2,key3
innerjson would be the name and value **
*/
//Alerts and logging of the variable.
console.log(innerjson); //should show you the value
alert(innerjson.name); //Should say xxxxxx,yyyyyy,zzzzzzz
});