0

I am trying to import data from multiple JSON files that have the same structure, into a singular python dictionary, so I can plot it on a line graph in PY. The real problem is that I'm a beginner and only know basics here. The JSON structure is something like this:

{
"ID": "123456",
"data": [
{ "xaxis" : 72, "yaxis" : 0 },
{ "xaxis" : 72.03814, "yaxis" : 0.00222175 },
{ "xaxis" : 72.08051, "yaxis" : 0.0044435},
{ "xaxis" : 72.12288, "yaxis" : 0.008887 },
{ "xaxis" : 72.16102, "yaxis" : 0.00666525 }
]
}

There are around 240 files like this, and what I'd like to do, is have the xaxis and yaxis data in the dictionary, so I can plot with it. Also I'd like to have the "ID" found in each file as, well, the ID of each dataset from each file. The lines should be on one graph from all the files, so one line is one file, so around 240 lines on the graph.

I would highly appreciate any help regarding this topic!

EDIT: The actual problem here seems to be for me to open the multiple json files one-by-one, and appending the data found in them to the dictionaries, I have gotten as far as to create dictionaries with hand-written data, but I don't know where to start with the multiple file openings, and creating a dictionary for each json file. I have found solutions for the other way around (dictionary to json), but not for this case.

How could I feed the code a path, where it can find the json files, go through them, and create a dict for each, in which there would be data as specified above?

Thanks!

2

1 Answer 1

1

Are you looking for something like

import matplotlib.pyplot as plt

d1  = {
"ID": "123456",
"data": [
{ "xaxis" : 72, "yaxis" : 0 },
{ "xaxis" : 72.03814, "yaxis" : 0.00222175 },
{ "xaxis" : 72.08051, "yaxis" : 0.0044435},
{ "xaxis" : 72.12288, "yaxis" : 0.008887 },
{ "xaxis" : 72.16102, "yaxis" : 0.00666525 }
]
}

d2  = {
"ID": "123456",
"data": [
{ "xaxis" : 72, "yaxis" : 0 },
{ "xaxis" : 72.03814, "yaxis" : 0.00622175 },
{ "xaxis" : 72.08051, "yaxis" : 0.0014435},
{ "xaxis" : 72.12288, "yaxis" : 0.003887 },
{ "xaxis" : 72.06102, "yaxis" : 0.00966525 }
]
}

for d in [d1, d2]:
    plt.plot([i['xaxis'] for i in d['data']], [i['yaxis'] for i in d['data']], label=d['ID'])

plt.legend()
plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, it would be something like this. But as there are many files I would try to go over them in a loop or something and somehow append to the dictionary each datapoint. So there should be as many d#'s as the number of the files (in this case around 240). Also the actual json file is much longer (around 120 lines), and copy pasting all 240 into the code would not be a pleasant thing to do, especially as this task might come up later again.
in for d in [d1, d2] you can append all them in [d1, d2, d3...] like creating a list l and using sppend

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.