var data = JSON.parse(value),
	ospf = [],
	ospf3 = [];

function getField(data, path, default_value) {
	var steps = path.split('.');
	var step = steps.shift();

	while (typeof step !== 'undefined') {
		if (typeof data === undefined || typeof data[step] === 'undefined') {
			return default_value;
		}

		data = data[step];
		step = steps.shift();
	}
	return data;
};

var ospf_data = getField(data, 'results.2.rpc-reply.ospf-neighbor-information-logical-system-all.logical-system', {});
var ospf3_data = getField(data, 'results.3.rpc-reply.ospf3-neighbor-information-logical-system-all.logical-system', {});

ospf_data.forEach(function (item) {
	var name = getField(item, 'name'),
		ospf_information = getField(item, 'ospf-neighbor-information', {}),
		ospf_neighbor = getField(ospf_information, 'ospf-neighbor', {}),
		ospf_neighbor = Array.isArray(ospf_neighbor) ? ospf_neighbor : [ospf_neighbor],
		output = getField(item, 'output');

	if (ospf_neighbor && output !== "OSPF instance is not running") {
		ospf_neighbor.forEach(function (neighbor) {
			ospf.push({
				router_name: name,
				neighbor_address: getField(neighbor, 'neighbor-address'),
				interface_name: getField(neighbor, 'interface-name'),
				uptime: getField(neighbor, 'neighbor-up-time.@seconds'),
				neighbor_state: getField(neighbor, 'ospf-neighbor-state'),
			});
		});
	}
});

ospf3_data.forEach(function (item) {
	var name = getField(item, 'name'),
		ospf_information = getField(item, 'ospf3-neighbor-information', {}),
		ospf_neighbor = getField(ospf_information, 'ospf3-neighbor', {}),
		ospf_neighbor = Array.isArray(ospf_neighbor) ? ospf_neighbor : [ospf_neighbor],
		output = getField(item, 'output');

	if (ospf_neighbor && output !== "OSPF instance is not running") {
		ospf_neighbor.forEach(function (neighbor) {
			ospf3.push({
				router_name: name,
				neighbor_address: getField(neighbor, 'neighbor-address'),
				interface_name: getField(neighbor, 'interface-name'),
				uptime: getField(neighbor, 'neighbor-up-time.@seconds'),
				neighbor_state: getField(neighbor, 'ospf-neighbor-state'),
			});
		});
	}
});

return JSON.stringify({ ospf, ospf3 });