Converting json like data to csv - json

data file looks like the below.
Note that the tags and values are not necesirly fixed (i.e. one file may have 10, next 30, 31 etc)
{
1=6487490
2=905629
3=14959
4=85
7=1
8=
9=
16=1
20=1252903800
21=557
22=13
}
i want output like this using python2
1, 2, 3,4,5,8,9,16,20,21,22
6487490,905629,14959,85,1,,,1,1252903800,557,13
where all the tags and values are converted to the csv structure.

Related

Chartist.js - data points not visible, issue with formatting json data

I am using Chartist.js to generate a chart dynamically using json data derived from multiple text inputs. The chart itself is generated with the proper labels series but no data points show, just the x and y axis. I've discovered that chartist needs an additional set of brackets '[ ]' around the data for it too work.
My json data looks like this:
{labels: ['07:00', '10:00', '12:00', '14:00'],
series: [333, 444, 322, 222]}
Chartist needs the series data to be written like this: series: [[333, 444, 322, 222]]
I am initiating Chartist like this:
new Chartist.Line('.ct-chart', data);
I need a bit of help in getting the extra brackets around the generated series data. I have searched for a method but all I can find are examples where the data is already set, not generated.
You need to split the series from your data, and assign them in their respective places.
var dataJson = {labels: ['07:00', '10:00', '12:00', '14:00'],
series: [333, 444, 322, 222]}
new Chartist.Line('.ct-chart', {
labels: dataJson.labels,
series: [dataJson.series]
}, {
fullWidth: true,
chartPadding: {
right: 40
}
});
Let me know if it worked.

Highcharts: How to show only one data »category« from CSV as a line

My CSV file has three columns:
Date, Values1, Values2
1880.0417, -183.0, 24.2
1880.1250, -171.1, 24.2
1880.2083, -164.3, 24.2
of which I want to display only the second one (Values1) as a line (chart).
I could prepare a CSV via Excel with only that and the date column. But due to ongoing work with the file, it would be much easier to get that CSV parsed while ignoring the second value.
Is that possible? I tried it with using the »series« parameter - but in vain.
Thanks a lot for any hints!
You can use seriesMapping property:
data: {
...,
seriesMapping: [{
x: 0,
y: 1
}, {}]
}
Live demo: https://jsfiddle.net/BlackLabel/tyLahrow/
API Reference: https://api.highcharts.com/gantt/data.seriesMapping

converting flattened json from list into dataframe in python

I have a nested json file which I managed to flatten, but as a result I got a list which looks like this:
[{'people_gender': 'Female',
'people_age_group': 'Young adult',
'people_distance': 91,
'time': 0.33},
{'people_gender': 'Male',
'people_age_group': 'Adult',
'people_distance': 88,
'time': 0.66}]
These are only two first instances of the list but there is of course no point of copying the whole list. Now I would like to convert it into the dataframe so the 'people_gender', 'people_age_group', 'people_distance' and 'time' are columns and in the rows are the results for respective time moments.
I simply tried:
df = pd.DataFrame(np.array(file))
but this just gives me the data frame with one column and in rows there are every entries for the given time moments and I don't know how to tackle it from there.
You can use json_normalize() to get this json in

Python3 Replacing special character from .csv file after convert the same from JSON

I am trying to develop a program using Python3.6.4 which convert a JSON file into a CSV file and also we need to clean the data in the csv file. as for example:
My JSON File:
{emp:[{"Name":"Bo#b","email":"bob#gmail.com","Des":"Unknown"},
{"Name":"Martin","email":"mar#tin#gmail.com","Des":"D#eveloper"}]}
Problem 1:
After converting that into csv its creating a blank row between every 2 rows. As
**Name email Des**
[<BLANK ROW>]
Bo#b bob#gmail.com Unknown
[<BLANK ROW>]
Martin mar#tin#gmail.com D#eveloper
Problem 2:
In my code I am using emp but I need to use it dynamically.
fobj = open("D:/Users/shamiks/PycharmProjects/jsonSamle.txt")
jsonCont = fobj.read()
print(jsonCont)
fobj.close()
employee_parsed = json.loads(jsonCont)
emp_data = employee_parsed['employee']
As we will not know the structure or content of up-coming JSON file.
Problem 3:
I also need to remove all # characters from the CSV file.
For solving Problem 3, you can use .replace (https://www.tutorialspoint.com/python/string_replace.htm).
For problem 2, you can use the dictionary keys and then get the zeroth item out of it.
fobj = open("D:/Users/shamiks/PycharmProjects/jsonSamle.txt")
jsonCont = fobj.read().replace("#", "")
print(jsonCont)
fobj.close()
employee_parsed = json.loads(jsonCont)
first_key = employee_parsed.keys()[0]
emp_data = employee_parsed[first_key]
I can't solve problem 1 without more code to see how your are exporting the result. It may be that your data has newlines in it. In which case, you could add .replace("\n","") and/or .replace("\r","") after the previous replace so the line would read fobj.read().replace("#", "").replace("\n", "").replace("\r", "").

JSON as input in a mapreduce

I have a JSON file contains fields such as machine_id, category, and ... Category contains states of machines such as "alarm", "failure". I simply like to see how many times each machine_id has been reported using rmr2.
For example, if I have the following:
machine_id, state
48, alarm
39, failure
48, utilization
I like to see this result:
48,2
39,1
What I did:
I wrote a simple mapreduce to read the value of JSON file and used it as an input in the second mapreduce. Code is:
mp = function(k,v){
machine_id=v$machine_id
keyval(machine_id,1) }
rd = function(k,v) keyval(k,length(v))
mapreduce(input = mapreduce(input='\user\cloudera\sample.json', input.format="json" , map=function(k,v) keyval(k,v)) , map=mp, reduce = rd)
Unfortunately, it returns only the last two values of JSON file. It seems that it doesn't read entire of the value of the JSON file. I would appreciate any help.