Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
JSON to CSV
#1
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
Reply


Messages In This Thread
JSON to CSV - by baluchen - Apr-17-2020, 11:06 AM
RE: JSON to CSV - by buran - Apr-17-2020, 11:21 AM
RE: JSON to CSV - by baluchen - Apr-17-2020, 11:31 AM
RE: JSON to CSV - by buran - Apr-17-2020, 11:35 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020