0

I have been trying to extract only certain data from a JSON file using python.I wanted to create an array from my json which has some entries as below

[{"Device Name":"abc", "Device Caps":["a1", "b1", "c1"]},
{"Device Name":"def", "Device Caps":["a2", "b2", "c2"]},
{"Device Name":"ghi", "Device Caps":["a3", "b3", "c3"]},
{"Device Name":"jkl", "Device Caps":["a4", "b4", "c4"]}] 

I need my output as ["a1","a2","a3","a4"]

3
  • [item["Device Caps"][0] for item in my_data] Commented Oct 18, 2016 at 17:13
  • So where did you get stuck with this? Note that once parsed, all you have is a list with dictionaries in it. Those are handled the same wherever they came from; this is not really a JSON problem. Commented Oct 18, 2016 at 17:14
  • 1
    Console throws error while executing this code. Error displayed below. one=[item["Device Caps"][0] for item in data] TypeError: string indices must be integers, not str Commented Oct 18, 2016 at 17:18

1 Answer 1

1

If that is literally your input file, then the following code will produce the output you want:

import json

with open("input.json") as input_file:
    data = json.load(input_file)
data = [d["Device Caps"][0] for d in data]

print(data)
Sign up to request clarification or add additional context in comments.

5 Comments

Traceback (most recent call last): File "C:\Users\injaleea\Desktop\test1.py", line 13, in <module> data = json.load(input_file) File "C:\Python27\lib\json_init_.py", line 290, in load **kw) File "C:\Python27\lib\json_init_.py", line 338, in loads return _default_decoder.decode(s) File "C:\Python27\lib\json\decoder.py", line 366, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Python27\lib\json\decoder.py", line 384, in raw_decode raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded
Traceback (most recent call last): File "C:\Users\injaleea\Desktop\test1.py", line 13, in <module> data = json.load(input_file) File "C:\Python27\lib\json_init_.py", line 290, in load **kw) File "C:\Python27\lib\json_init_.py", line 338, in loads return _default_decoder.decode(s) File "C:\Python27\lib\json\decoder.py", line 366, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Python27\lib\json\decoder.py", line 384, in raw_decode raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded
Returned the error below Traceback (most recent call last): File "C:\Users\injaleea\Desktop\test1.py", line 13, in <module>data = json.load(input_file)File "C:\Python27\lib\json\__init__.py", line 290, in load**kw) File "C:\Python27\lib\json\__init__.py", line 338, in loadreturn _default_decoder.decode(s) File "C:\Python27\lib\json\decoder.py", line 366, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Python27\lib\json\decoder.py", line 384, in raw_decode raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded
From that error message, I infer that what you posted is not your JSON file.
You were right.My json had some more comment section on top. Is there a way to ignore them ?

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.