Python Forum

Full Version: JSON to CSV
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Dear all,

I just started learning Python and got the requirement to format json data into csv. I tried with Pandas and it seems it doesn't work with nested json. So I'm looking for help here.

Below is my json output and that needs to be converted into csv

{
	'facets': [{
		'name': ['alertmanager-4565cfdbfc-kzhjw',
		'kube-ops'],
		'results': [{
			'result': 0.0
		},
		{
			'result': 0.0
		},
		{
			'latest': 0.0
		}]
	},
	{
		'name': ['calico-node-d4pdr',
		'kube-system'],
		'results': [{
			'result': 0.0
		},
		{
			'result': 0.0
		},
		{
			'latest': 0.0
		}]
	},
	{
		'name': ['calico-node-gbdt7',
		'kube-system'],
		'results': [{
			'result': 0.0
		},
		{
			'result': 0.0
		},
		{
			'latest': 0.0
		}]
	},
	{
		'name': ['calico-node-gzqc4',
		'kube-system'],
		'results': [{
			'result': 0.0
		},
		{
			'result': 0.0
		},
		{
			'latest': 0.0
		}]
	},
	{
		'name': ['calico-node-pwk9t',
		'kube-system'],
		'results': [{
			'result': 0.0
		},
		{
			'result': 0.0
		},
		{
			'latest': 0.0
		}]
	},
	{
		'name': ['calico-node-pwsl8',
		'kube-system'],
		'results': [{
			'result': 0.0
		},
		{
			'result': 0.0
		},
		{
			'latest': 0.0
		}]
	},
	{
		'name': ['normalconnect-7d48c69f5f-ng8k4',
		'kube-ops'],
		'results': [{
			'result': 0.0
		},
		{
			'result': 0.0
		},
		{
			'latest': 0.0
		}]
	}],
	'totalResult': {
		'results': [{
			'result': 0.0
		},
		{
			'result': 0.0
		},
		{
			'latest': 0.0
		}]
	},
	'unknownGroup': {
		'results': [{
			'result': None
		},
		{
			'result': None
		},
		{
			'latest': None
		}]
	},
	'performanceStats': {
		'inspectedCount': 18710013,
		'omittedCount': 0,
		'matchCount': 1884,
		'wallClockTime': 262
	},
	'metadata': {
		'eventTypes': ['PodSample'],
		'eventType': 'PodSample',
		'openEnded': True,
		'beginTime': '2020-04-17T08: 51: 26Z',
		'endTime': '2020-04-17T09: 00: 26Z',
		'beginTimeMillis': 1587113486899,
		'endTimeMillis': 1587114026899,
		'rawSince': '10MINUTESAGO',
		'rawUntil': '1MINUTESAGO',
		'rawCompareWith': '',
		'guid': '5f781c12423-c730-33a3-5668-c58bc2342345261',
		'routerGuid': 'ca02346df-8f29-ad23-4ac5-03241004dc9f64',
		'messages': [],
		'facet': ['podName',
		'namespace'],
		'offset': 0,
		'limit': 2000,
		'contents': {
			'messages': [],
			'contents': [{
				'function': 'alias',
				'alias': 'ReceivedKBps',
				'contents': {
					'function': 'binop',
					'simple': True,
					'binop': '/',
					'left': {
						'function': 'latest',
						'attribute': 'net.rxBytesPerSecond',
						'simple': True
					},
					'right': {
						'constant': 1000.0
					}
				}
			},
			{
				'function': 'alias',
				'alias': 'TransmittedKBps',
				'contents': {
					'function': 'binop',
					'simple': True,
					'binop': '/',
					'left': {
						'function': 'latest',
						'attribute': 'net.txBytesPerSecond',
						'simple': True
					},
					'right': {
						'constant': 1000.0
					}
				}
			},
			{
				'function': 'alias',
				'alias': 'Errors/sec',
				'contents': {
					'function': 'latest',
					'attribute': 'net.errorsPerSecond',
					'simple': True
				}
			}]
		}
	}
}


Expected output in csv

Output:
Pod Name Namespace Received KBps Transmitted KBps Errors / sec calico-node-pwsl8 kube-system 0 0 0 calico-node-gzqc4 kube-system 0 0 0 alertmanager-45fdbfc-kzhjw kube-ops 0 0 0 fdafhsdfgfdsg kube-ops 0 0 0
look at json module. you need to parse it your self if pandas does not produce what you want
(Apr-17-2020, 11:21 AM)buran Wrote: [ -> ]look at json module. you need to parse it your self if pandas does not produce what you want

I tried with Pandas and it worked with simple json and not with nested. That's why I'm here
(Apr-17-2020, 11:31 AM)baluchen Wrote: [ -> ]I tried with Pandas and it worked with simple json and not with nested. That's why I'm here
and that's why you get an advise - look at json module and parse the json yourself. We are glad to help, but we are not great at writing code for others.