I need to parse a JSON file in JavaScript containing data for a power system. The object (loaded_file) has the following keys:
['area_interchanges', 'buses', 'facts', 'fixed_shunts', 'generators', 'hvdc_lines', 'induction_machines', 'interarea_transfers', 'lines', 'loads', 'multi_section_line', 'multi_terminal_hvdc_lines', 'owners', 'switched_shunts', 'switching_devices', 'system_info', 'transformer_tables', 'transformers', 'vsc_dc_lines', 'zones'])
Each of these represents a different type of component. Only buses, lines, and transformers are needed.
Each key maps to an array of JSON objects of that type. For example, this is what one bus object looks like:
loaded_file[‘buses’][0] = {'area': 1, 'baskv': 138, 'evhi': 1.1,
'evlo': 0.9, 'infeasIi': 0, 'infeasIr': 0, 'infeasP': 0, 'infeasQ': 0,
'is_dangling': False, 'lat': 0, 'lon': 0, 'name': "'NEAH BAY 1 '",
'number': 10001, 'nvhi': 1.1, 'nvlo': 0.9, 'owner': 1, 'type': 1,
'va': -39.151686, 'vm': 1.01167512, 'zone': 1}
Each of the keys of that object represent attributes of a bus. Here is an example of a line object:
loaded_file[‘lines’][0] = {'b': 0.0455668, 'bi': 0, 'bj': 0, 'ckt':
"'1 '", 'gi': 0, 'gj': 0, 'i': 10002, 'j': 10001, 'len': 46.8,
'metered_end': 1, 'owner': [1, 0, 0, 0], 'owner_fraction': [1, 1, 1,
1], 'r': 0.0574738, 'ratings': [185.33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0], 'status': True, 'x': 0.297874}
And here is a transformer:
loaded_file[‘transformers’][0] = {'ang': [0, 0, 0], 'base0': [138, 0,
0], 'base1': [345, 0, 0], 'bmag': 0, 'ckt': "'1 '", 'cm': 1, 'cod':
[0, 0, 0], 'cont': [10013, 0, 0], 'cw': 1, 'cz': 1, 'gmag': 0, 'i':
10013, 'is_in_contingency': False, 'j': 10012, 'k': 0, 'mag1': 0,
'mag2': 0, 'name': "' '", 'nod': [-1, -1, -1], 'nomv': [138,
345, 1], 'non_metered_end': 2, 'ntp': [33, 33, 33], 'oscillated':
False, 'owner': [1, 0, 0, 0], 'owner_fraction': [1, 1, 1, 1], 'r_1_2':
0.00026094, 'r_2_3': 0, 'r_3_1': 0, 'ratings': [[473.63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0]], 'rloss': [0.00026094, 0, 0], 'rma': [1.1,
1.1, 1.1], 'rmi': [0.9, 0.9, 0.9], 'sbase_1_2': 100, 'sbase_2_3': 100, 'sbase_3_1': 100, 'status': 1, 'table': [0, 0, 0], 'tr': [1, 0, 0],
'va_star': 0, 'vm_star': 1, 'vma': [1.01, 1.1, 1.1], 'vmi': [0.99,
0.9, 0.9], 'wbase0': [138, 0, 0], 'wbase1': [345, 0, 0], 'winding_status': [True, False, False], 'windv': [1, 1, 1], 'x_1_2':
0.0193438, 'x_2_3': 0, 'x_3_1': 0, 'xloss': [0.0193438, 0, 0]}
The only attributes I need for each type of device are the following:
buses: number
lines: i, j
transformers: i, j, k
Each bus will be a node in the graph, identified by its number. Lines and transformers are the edges: i and j correspond to “from” and “to” buses of the edge. If more than one edge exists between two nodes, a single edge needs to be drawn. Transformers may also have a k bus – indicated by a nonzero value – and if this is encountered, three edges must be built: i-j, j-k, and k-i. If the k bus is 0, ignore it. If an edge has an i, j, or k bus that does not exist, the edge is ignored.
So far the console is showing only buses, lines, and transformers as expected. But how do I show the attributes for each?
Here is my code:
"use strict";
let bus, line, transformer = [], loaded_file = {};
let show_loaded_file = function() {
for (let prop in loaded_file) {
console.log(prop);
console.log(loaded_file[prop]);
};
}
fetch("./sample_data.json")
.then(function(resp) {
return resp.json();
})
.then(function(data) {
console.log(data);
bus = data.buses;
line = data.lines;
transformer = data.transformers;
loaded_file = data;
show_loaded_file();
});