1

I am new to python.

I have two set of JSON output files and I need to convert them to csv or dataframe.

File 1
{
"lastFileTime" : "2020-06-08T00:23:05.986-07:00",
"lastFileTimeMs" : "1591600985986",
"statNames" : ["stat1", "stat2", "stat3", "stat4", "stat5", "stat6", "stat7", "stat8", "stat9"],
"values" : [[1, 2, 3, 4, 5, 6, 7, 8, 9],[9, 8, 7, 6, 5, 4, 3, 2, 1]]
}

Desired Output
stat1,stat2,stat3,stat4,stat5,stat6,stat7,stat8,stat9
1,2,3,4,5,6,7,8,9
9,8,7,6,5,4,3,2,1

So I need to discard lastFileTime and lastFileTimeMs and read display in csv. I tried the following:

# Import the json module
import json

# Open a json file into a json object
with open('a.json') as F:
    json_data = json.loads(F.read())

# Print loaded json object
print(json_data)

But I keep getting:

Traceback (most recent call last):
  File "t.py", line 9, in <module>
    json_data = json.loads(F.read())
  File "/usr/lib64/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib64/python2.7/json/decoder.py", line 384, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

Any help is appreciated... The other problem I have is that I am tied to python 2.7.5 :(

1 Answer 1

2
import json
import itertools

if __name__ == "__main__":
    jsons = json.load(open("config2.json"))
    lastFileTime = jsons["lastFileTime"]
    lastFileTimeMs = jsons["lastFileTimeMs"]
    statNames = jsons["statNames"]
    values = jsons["values"]
    print(lastFileTime)
    print(lastFileTimeMs)
    list_to_str_statNames = ' '.join([str(element) for element in statNames])
    print(str(list_to_str_statNames))
    v1 = values[0]
    list_to_str_v1 = ' '.join([str(element) for element in v1])
    print(list_to_str_v1)
    v2 = values[1]
    list_to_str_v2 = ' '.join([str(element) for element in v2])
    print(list_to_str_v2)
Sign up to request clarification or add additional context in comments.

6 Comments

This code will work on Python 2.7 and 3+. Make a note of jsons variable and json package. jsons = json.load(open("config.json"))
Hi, thanks but the output is coming like: # python s.py 2020-06-08T00:23:05.986-07:00 1591600985986 stat1 stat2 stat3 stat4 stat5 stat6 stat7 stat8 stat9 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1
comment these lines. #print(lastFileTime) #print(lastFileTimeMs) and add below line print("\n")
code has been updated 2020-06-08T00:23:05.986-07:00 1591600985986 stat1 stat2 stat3 stat4 stat5 stat6 stat7 stat8 stat9 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1
To store it in dataframe.. from pandas.io.json import json_normalize df = pd.DataFrame.from_dict(json_normalize(jsons)) print(df)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.