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
#2
look at json module. you need to parse it your self if pandas does not produce what you want
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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
Reply
#4
(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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

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