Extracting an object among many, from JSON file in python - python
My JSON file (5000 in total) looks like this
{"version":1.2,"people":[{"pose_keypoints_2d":[0,0,0,1383.9,318.242,0.892951,1427.93,327.25,0.828307,1445.77,409.619,0.952105,1416.18,397.863,0.090918,1345.49,306.583,0.789117,0,0,0,0,0,0,1377.87,459.777,0.824136,1404.51,471.452,0.745714,0,0,0,0,0,0,1345.6,459.6,0.731992,0,0,0,0,0,0,0,0,0,0,0,0,1421.95,271.437,0.540316,1377.91,268.169,0.306764,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"face_keypoints_2d":[],"hand_left_keypoints_2d":[],"hand_right_keypoints_2d":[],"pose_keypoints_3d":[],"face_keypoints_3d":[],"hand_left_keypoints_3d":[],"hand_right_keypoints_3d":[]},{"pose_keypoints_2d":[1516.22,174.003,0.932503,1507.48,224.333,0.864362,1451.52,221.257,0.883005,1413.29,291.958,0.80518,1463.28,291.779,0.867427,1563.37,238.898,0.84872,1569.37,321.275,0.825046,1566.2,394.961,0.864594,1495.57,412.46,0.804823,1466.11,409.65,0.698038,1454.5,527.379,0.146485,0,0,0,1528.09,412.567,0.733411,1516.28,530.372,0.196314,0,0,0,1504.51,156.44,0.878404,1525.11,159.409,0.852905,1492.74,159.449,0.567964,1542.82,171.264,0.637156,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"face_keypoints_2d":[],"hand_left_keypoints_2d":[],"hand_right_keypoints_2d":[],"pose_keypoints_3d":[],"face_keypoints_3d":[],"hand_left_keypoints_3d":[],"hand_right_keypoints_3d":[]},{"pose_keypoints_2d":[630.433,247.712,0.887763,621.52,297.744,0.898341,580.278,297.779,0.803383,600.839,380.229,0.766635,624.594,324.31,0.850614,665.583,294.92,0.862791,677.475,377.217,0.792423,644.959,389.091,0.861932,630.352,459.596,0.708987,606.759,459.742,0.639099,612.697,580.392,0.424434,594.964,809.933,0.445884,659.764,456.741,0.742195,653.902,577.364,0.655682,648.036,689.252,0.638049,621.585,241.786,0.917391,642.178,238.881,0.869318,597.994,244.66,0.8782,648.082,238.926,0.0987843,674.549,715.811,0.559119,677.444,712.729,0.545488,642.173,695.197,0.447904,648.119,833.326,0.4606,639.217,836.383,0.40265,583.314,830.54,0.387601],"face_keypoints_2d":[],"hand_left_keypoints_2d":[],"hand_right_keypoints_2d":[],"pose_keypoints_3d":[],"face_keypoints_3d":[],"hand_left_keypoints_3d":[],"hand_right_keypoints_3d":[]},{"pose_keypoints_2d":[0,0,0,1024.74,274.253,0.879164,1071.92,280.108,0.773811,1092.51,365.365,0.638758,0,0,0,977.61,271.255,0.881595,974.632,342.017,0.124485,0,0,0,1018.81,439.052,0.685377,1048.31,441.989,0.674454,1039.42,553.779,0.665407,1018.85,662.676,0.66249,980.631,436.208,0.673822,983.535,547.897,0.543447,974.669,647.981,0.465162,0,0,0,0,0,0,1068.95,224.063,0.535875,1021.84,215.334,0.331974,989.444,665.68,0.120769,962.987,650.998,0.121694,971.792,665.614,0.374614,1036.49,668.732,0.40357,1036.47,671.618,0.45803,1007.05,671.574,0.609372],"face_keypoints_2d":[],"hand_left_keypoints_2d":[],"hand_right_keypoints_2d":[],"pose_keypoints_3d":[],"face_keypoints_3d":[],"hand_left_keypoints_3d":[],"hand_right_keypoints_3d":[]},{"pose_keypoints_2d":[1133.66,156.449,0.871671,1160.14,200.52,0.909641,1119.08,194.716,0.881631,1101.34,250.727,0.741804,1095.47,306.6,0.554203,1189.7,206.51,0.863547,1210.3,294.77,0.795075,1201.36,359.55,0.150867,1142.45,356.582,0.705377,1118.83,350.711,0.660271,1118.81,450.822,0.10509,0,0,0,1166.08,359.521,0.703378,1160.13,439.008,0.0764702,0,0,0,1124.82,150.485,0.869508,1142.39,150.587,0.958235,0,0,0,1166.1,150.462,0.915307,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"face_keypoints_2d":[],"hand_left_keypoints_2d":[],"hand_right_keypoints_2d":[],"pose_keypoints_3d":[],"face_keypoints_3d":[],"hand_left_keypoints_3d":[],"hand_right_keypoints_3d":[]},{"pose_keypoints_2d":[1327.76,344.854,0.7714,1230.79,412.576,0.777522,1186.54,430.265,0.710346,1166.08,574.291,0.745479,1286.74,551.003,0.859514,1274.9,400.802,0.615538,1333.89,500.898,0.732315,1330.99,406.568,0.842077,1251.33,692.147,0.54616,1213.11,695.104,0.558347,1195.5,874.656,0.59577,1151.27,1045.3,0.537499,1283.75,689.225,0.499876,1260.23,868.795,0.569042,1213.25,1018.88,0.64621,1310.24,338.859,0.914034,0,0,0,1260.32,342.03,0.923518,0,0,0,1283.71,1048.34,0.615529,1283.79,1045.27,0.588761,1201.34,1030.64,0.34822,1218.98,1054.23,0.162428,0,0,0,1139.5,1051.25,0.387906],"face_keypoints_2d":[],"hand_left_keypoints_2d":[],"hand_right_keypoints_2d":[],"pose_keypoints_3d":[],"face_keypoints_3d":[],"hand_left_keypoints_3d":[],"hand_right_keypoints_3d":[]},{"pose_keypoints_2d":[851.14,230.059,0.836269,907,300.74,0.759377,886.42,294.972,0.670902,880.598,377.227,0.139703,839.35,365.599,0.321698,927.62,306.627,0.803614,921.722,409.668,0.740124,854.059,388.947,0.874953,886.524,486.13,0.63616,883.497,483.296,0.584742,903.968,624.47,0.744024,912.915,739.165,0.769062,895.273,489.101,0.678105,880.593,627.447,0.80521,880.369,789.348,0.833229,0,0,0,859.958,221.314,0.9495,0,0,0,901.129,227.162,0.8659,821.619,812.882,0.762655,839.321,818.779,0.748007,886.466,809.794,0.762739,865.847,745.129,0.178172,0,0,0,927.672,745.158,0.699705],"face_keypoints_2d":[],"hand_left_keypoints_2d":[],"hand_right_keypoints_2d":[],"pose_keypoints_3d":[],"face_keypoints_3d":[],"hand_left_keypoints_3d":[],"hand_right_keypoints_3d":[]},{"pose_keypoints_2d":[1748.81,400.745,0.865472,1769.41,480.169,0.60199,1692.83,453.801,0.777207,1636.84,571.465,0.602594,1622.26,674.491,0.237392,1831.2,512.658,0.44287,1751.68,715.693,0.0641964,0,0,0,1689.96,718.654,0.295779,1645.81,703.916,0.317921,1607.49,854.024,0.214191,1572.18,1001.31,0.419405,1734.12,733.402,0.223622,1613.47,856.961,0.100664,1580.98,1010.01,0.141665,1745.88,377.234,0.802295,1772.39,397.828,0.831415,0,0,0,1819.34,433.244,0.858218,1489.73,1024.81,0.22791,1498.66,1036.55,0.219253,1589.85,1030.66,0.174787,1492.65,1024.84,0.494689,1492.73,1021.76,0.468759,1586.97,1024.84,0.321146],"face_keypoints_2d":[],"hand_left_keypoints_2d":[],"hand_right_keypoints_2d":[],"pose_keypoints_3d":[],"face_keypoints_3d":[],"hand_left_keypoints_3d":[],"hand_right_keypoints_3d":[]},{"pose_keypoints_2d":[0,0,0,547.977,341.978,0.246313,536.227,341.878,0.601369,550.96,450.742,0.778364,615.651,424.273,0.762666,0,0,0,0,0,0,0,0,0,580.284,530.316,0.318203,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"face_keypoints_2d":[],"hand_left_keypoints_2d":[],"hand_right_keypoints_2d":[],"pose_keypoints_3d":[],"face_keypoints_3d":[],"hand_left_keypoints_3d":[],"hand_right_keypoints_3d":[]},{"pose_keypoints_2d":[1831.27,265.312,0.718134,1781.17,291.987,0.733738,1748.81,294.957,0.794407,1719.31,388.954,0.679386,1748.76,418.448,0.11695,1822.32,288.843,0.617834,0,0,0,0,0,0,1754.63,441.953,0.0559453,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1825.18,247.9,0.881403,1834.11,247.735,0.108933,1790.05,247.868,0.858578,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"face_keypoints_2d":[],"hand_left_keypoints_2d":[],"hand_right_keypoints_2d":[],"pose_keypoints_3d":[],"face_keypoints_3d":[],"hand_left_keypoints_3d":[],"hand_right_keypoints_3d":[]},{"pose_keypoints_2d":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,577.393,527.334,0.376305,580.359,671.567,0.278565,618.596,692.074,0.379358,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,624.434,700.97,0.323246],"face_keypoints_2d":[],"hand_left_keypoints_2d":[],"hand_right_keypoints_2d":[],"pose_keypoints_3d":[],"face_keypoints_3d":[],"hand_left_keypoints_3d":[],"hand_right_keypoints_3d":[]},{"pose_keypoints_2d":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,580.365,533.218,0.257544,600.889,686.267,0.313003,600.939,809.808,0.41703,0,0,0,0,0,0,0,0,0,0,0,0,650.978,833.455,0.42398,648.014,827.561,0.372851,589.17,830.452,0.229458,0,0,0,0,0,0,0,0,0],"face_keypoints_2d":[],"hand_left_keypoints_2d":[],"hand_right_keypoints_2d":[],"pose_keypoints_3d":[],"face_keypoints_3d":[],"hand_left_keypoints_3d":[],"hand_right_keypoints_3d":[]},{"pose_keypoints_2d":[930.432,177.066,0.270157,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,927.496,174.034,0.370837,0,0,0,903.997,174.152,0.297842,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"face_keypoints_2d":[],"hand_left_keypoints_2d":[],"hand_right_keypoints_2d":[],"pose_keypoints_3d":[],"face_keypoints_3d":[],"hand_left_keypoints_3d":[],"hand_right_keypoints_3d":[]}],"part_candidates":[{"0":[1133.66,156.449,0.871671,1516.22,174.003,0.932503,930.432,177.066,0.270157,851.14,230.059,0.836269,630.433,247.712,0.887763,1831.27,265.312,0.718134,1327.76,344.854,0.7714,1748.81,400.745,0.865472],"1":[1160.14,200.52,0.909641,1507.48,224.333,0.864362,1024.74,274.253,0.879164,1781.17,291.987,0.733738,621.52,297.744,0.898341,907,300.74,0.759377,1383.9,318.242,0.892951,547.977,341.978,0.246313,1230.79,412.576,0.777522,1769.41,480.169,0.60199,1869.48,553.935,0.143333],"2":[1119.08,194.716,0.881631,1451.52,221.257,0.883005,1071.92,280.108,0.773811,886.42,294.972,0.670902,1748.81,294.957,0.794407,580.278,297.779,0.803383,1427.93,327.25,0.828307,536.227,341.878,0.601369,1186.54,430.265,0.710346,1692.83,453.801,0.777207,1922.51,533.203,0.0694578],"3":[1101.34,250.727,0.741804,1413.29,291.958,0.80518,1092.51,365.365,0.638758,880.598,377.227,0.139703,600.839,380.229,0.766635,1719.31,388.954,0.679386,1445.77,409.619,0.952105,550.96,450.742,0.778364,1636.84,571.465,0.602594,1166.08,574.291,0.745479],"4":[1463.28,291.779,0.867427,1095.47,306.6,0.554203,624.594,324.31,0.850614,839.35,365.599,0.321698,1416.18,397.863,0.090918,1748.76,418.448,0.11695,615.651,424.273,0.762666,1286.74,551.003,0.859514,1622.26,674.491,0.237392],"5":[1189.7,206.51,0.863547,1563.37,238.898,0.84872,977.61,271.255,0.881595,1822.32,288.843,0.617834,665.583,294.92,0.862791,927.62,306.627,0.803614,1345.49,306.583,0.789117,545.017,338.899,0.173114,1274.9,400.802,0.615538,1831.2,512.658,0.44287],"6":[1210.3,294.77,0.795075,1569.37,321.275,0.825046,974.632,342.017,0.124485,677.475,377.217,0.792423,921.722,409.668,0.740124,1333.89,500.898,0.732315,1751.68,715.693,0.0641964,1734.09,736.304,0.0629545,1704.59,974.704,0.0607181],"7":[1201.36,359.55,0.150867,644.959,389.091,0.861932,854.059,388.947,0.874953,1566.2,394.961,0.864594,1330.99,406.568,0.842077,1704.67,662.737,0.0836309],"8":[1142.45,356.582,0.705377,1495.57,412.46,0.804823,1018.81,439.052,0.685377,1754.63,441.953,0.0559453,630.352,459.596,0.708987,1377.87,459.777,0.824136,886.524,486.13,0.63616,580.284,530.316,0.318203,1251.33,692.147,0.54616,1689.96,718.654,0.295779,1863.57,992.377,0.0562326],"9":[1118.83,350.711,0.660271,1466.11,409.65,0.698038,1048.31,441.989,0.674454,606.759,459.742,0.639099,1404.51,471.452,0.745714,883.497,483.296,0.584742,577.393,527.334,0.376305,1213.11,695.104,0.558347,1645.81,703.916,0.317921],"10":[1118.81,450.822,0.10509,1454.5,527.379,0.146485,1039.42,553.779,0.665407,612.697,580.392,0.424434,903.968,624.47,0.744024,580.359,671.567,0.278565,1607.49,854.024,0.214191,1195.5,874.656,0.59577],"11":[1018.85,662.676,0.66249,618.596,692.074,0.379358,912.915,739.165,0.769062,594.964,809.933,0.445884,1572.18,1001.31,0.419405,1151.27,1045.3,0.537499],"12":[1166.08,359.521,0.703378,1528.09,412.567,0.733411,980.631,436.208,0.673822,659.764,456.741,0.742195,1345.6,459.6,0.731992,895.273,489.101,0.678105,580.365,533.218,0.257544,1283.75,689.225,0.499876,1734.12,733.402,0.223622,1795.89,980.592,0.0555809,1681.09,1074.83,0.0648456],"13":[1160.13,439.008,0.0764702,1516.28,530.372,0.196314,983.535,547.897,0.543447,653.902,577.364,0.655682,880.593,627.447,0.80521,600.889,686.267,0.313003,1613.47,856.961,0.100664,1260.23,868.795,0.569042],"14":[974.669,647.981,0.465162,648.036,689.252,0.638049,880.369,789.348,0.833229,600.939,809.808,0.41703,1580.98,1010.01,0.141665,1213.25,1018.88,0.64621,1663.41,1074.84,0.076733],"15":[1124.82,150.485,0.869508,1504.51,156.44,0.878404,927.496,174.034,0.370837,856.812,221.238,0.0621831,621.585,241.786,0.917391,1825.18,247.9,0.881403,1310.24,338.859,0.914034,1745.88,377.234,0.802295],"16":[1142.39,150.587,0.958235,1525.11,159.409,0.852905,930.546,174.006,0.13775,859.958,221.314,0.9495,642.178,238.881,0.869318,1834.11,247.735,0.108933,1330.78,330.13,0.111218,1772.39,397.828,0.831415],"17":[1492.74,159.449,0.567964,903.997,174.152,0.297842,1068.95,224.063,0.535875,597.994,244.66,0.8782,1790.05,247.868,0.858578,1421.95,271.437,0.540316,1260.32,342.03,0.923518],"18":[1166.1,150.462,0.915307,1542.82,171.264,0.637156,1021.84,215.334,0.331974,901.129,227.162,0.8659,648.082,238.926,0.0987843,1377.91,268.169,0.306764,1819.34,433.244,0.858218],"19":[989.444,665.68,0.120769,674.549,715.811,0.559119,821.619,812.882,0.762655,650.978,833.455,0.42398,1489.73,1024.81,0.22791,1283.71,1048.34,0.615529],"20":[962.987,650.998,0.121694,677.444,712.729,0.545488,839.321,818.779,0.748007,648.014,827.561,0.372851,1498.66,1036.55,0.219253,1283.79,1045.27,0.588761],"21":[971.792,665.614,0.374614,642.173,695.197,0.447904,886.466,809.794,0.762739,589.17,830.452,0.229458,1201.34,1030.64,0.34822,1589.85,1030.66,0.174787,1660.5,1074.84,0.0597874],"22":[1036.49,668.732,0.40357,668.602,715.634,0.124469,865.847,745.129,0.178172,648.119,833.326,0.4606,1492.65,1024.84,0.494689,1224.82,1051.34,0.162429,1218.98,1054.23,0.162428,1168.98,1074.84,0.174282,1198.4,1074.81,0.16778],"23":[1036.47,671.618,0.45803,650.994,715.785,0.112767,871.693,739.271,0.162696,639.217,836.383,0.40265,1492.73,1021.76,0.468759,1166.04,1074.81,0.239277],"24":[1007.05,671.574,0.609372,1424.99,695.172,0.0523207,624.434,700.97,0.323246,927.672,745.158,0.699705,583.314,830.54,0.387601,1586.97,1024.84,0.321146,1139.5,1051.25,0.387906]}]}
I need to extract only one object named "1" with its contents and save these to a new JSON file with a new name say, "New file 1", "New file 2" ....
Or if deleting all contents except "1" and its contents, and leaving the file as it is with object "1".
You can use the json library to read the file contents into a python data structure (in this case, a nested dictionary). You can them dump specific parts of the JSON data out into new files.
import json
with open('input_json.json', 'r') as infile:
data = json.load(infile)
for k, v in data['part_candidates'][0].items():
with open('output_json_{}.json'.format(k), 'w') as outfile:
json.dump(v, outfile)
convert Json to Dictionary, using python library json
import json
data = json.loads(your json)
Than after you are done performing
json.dump(data)
Edit 1
You can access the element in a Dictionary by
sample_data = dict((k,data[k]) for k in ('key1','key2','key99') if k in data)
Related
Is there a way to iterate over a JSON file and write specific keys and values to a new JSON file?
I'm trying to iterate over specific keys and values from a JSON file and write them to a new JSON file. I'm not getting any errors and the second JSON file is being created successfully, though the data passed onto the second JSON file is a whole object of the first JSON file. def get_keys_values(): json_file = open("get_failed_archives.json") json_data = json.load(json_file) for archive_data in json_data["data"]: archive_data["latestEvent"]["objectName"] archive_data["latestEvent"]["time"] archive_data["latestEvent"]["eventType"] archive_data["latestEvent"]["eventStatus"] with open("new_json_file.json", "w") as file: json.dump(archive_data, file) Is there a way to write specific keys and values from one JSON file to another JSON file?
as stated by josh, the statement archive_data["latestEvent"]["objectName"] functionally does nothing. if you want to create a new JSON object, in your case this would functionally be a list of dict items (I think), the approach below would match what (I think) you want it to do. I've added a pprint statement to print out what the result is. import json from pprint import pprint def get_keys_values(): with open("get_failed_archives.json") as json_file: json_data = json.load(json_file) new_data = [] for archive_data in json_data["data"]: new_data.append({ "objectName": archive_data["latestEvent"]["objectName"], "time": archive_data["latestEvent"]["time"], "eventType": archive_data["latestEvent"]["eventType"], "eventStatus": archive_data["latestEvent"]["eventStatus"] }) pprint(new_data, indent=4) with open("new_json_file.json", "w") as file: json.dump(new_data, file)
Writing multiple lists into JSON file
I'm able to store a single list into a json file to be used later. How would you store multiple lists into a json file. Let's say that I wanted to add the username to the file and the username + '#gmail.com" as another list into the same file. How would you access these two lists from the json file later? import json username = johnsmith91 storelist = [] storelist.append(username) with open('username.json', 'a') as outfile: json.dump(storelist, outfile)
Load JSON file into a dictionary and not string or list
I have created a JSON file after scraping data online with the following simplified code: for item in range(items_to_scrape) az_text = [] for n in range(first_web_page, last_web_page): reviews_html = requests.get(page_link) tree = fromstring(reviews_html.text) page_link = base_url + str(n) review_text_tags = tree.xpath(xpath_1) for r_text in review_text_tags: review_text = r_text.text az_text.append(review_text) az_reviews = {} az_reviews[item] = az_text with open('data.json', 'w') as outfile: json.dump(az_reviews , outfile) There might be a better way to create a JSON file with the first key equal to the item number and the second key equal to the list of reviews for that item, however I am currently stuck at opening the JSON file to see the items have been already scraped. The structure of the JSON file looks like this: { "asin": "0439785960", "reviews": [ "Don’t miss this one!", "Came in great condition, one of my favorites in the HP series!", "Don’t know how these books are so good and I’ve never read them until now. Whether you’ve watched the movies or not, read these books" ] } The unsuccessful attempt that seems to be closer to the solution is the following: import json from pprint import pprint json_data = open('data.json', 'r').read() json1_file = json.loads(json_data) print(type(json1_file)) print(json1_file["asin"]) It returns a string that replicates exactly the result of the print() function I used during the scraping process to check what the JSON file was going to be look like, but I can't access the asins or reviews using json1_file["asin"] or json1_file["reviews"] since the file read is a string and not a dictionary. TypeError: string indices must be integers Using the json.load() function I still print the right content, but I have cannot figure out how to access the dictionary-like object from the JSON file to iterate through keys and values. The following code prints the content of the file, but raises an error (AttributeError: '_io.TextIOWrapper' object has no attribute 'items') when I try to iterate through keys and values: with open('data.json', 'r') as content: print(json.load(content)) for key, value in content.items(): print(key, value) What is wrong with the code above and what should be adjusted to load the file into a dictionary?
string indices must be integers You're writing out the data as a string, not a dictionary. Remove the dumps, and only dump with open('data.json', 'w') as outfile: json.dump(az_reviews, outfile, indent=2, ensure_ascii=False) what should be adjusted to load the file into a dictionary? Once you're parsing a JSON object, and not a string, then nothing except maybe not using reads, then loads and rather only json.load Another problem seems to be that you're overwriting the file on every loop iteration Instead, you probably want to open one file then loop and write to it afterwards data = {} for item in range(items_to_scrape): pass # add to data # put all data in one file with open('data.json', 'w') as f: json.dump(data, f) In this scenario, I suggest that you store the asin as a key, with the reviews as values asin = "123456" # some scraped value data[asin] = reviews Or write a unique file for each scrape, which you then must loop over to read them all. for item in range(items_to_scrape): data = {} # add to data with open('data{}.json'.format(item), 'w') as f: json.dump(data, f)
Read JSON file in python: ValueError
I have a .txt file with 70+k json object obtained by pulling data from twitter and dumping into file using: with open("followers.txt", 'a') as f: for follower in limit_handled(tweepy.Cursor(api.followers, screen_name=account_name).pages()): for user_obj in follower: json.dump(user_obj._json, f) f.write("\n") When I try to read this in python using the code below: import json with open('followers.txt') as json_data: follower_data = json.load(json_data) I get error: ValueError: Extra data: line 2 column 1 - line 2801 column 1 (char 1489 - 8679498) It worked when I read a test file with one json object copied from the original file using the same code above. Once I add a second json object to this file then using the same code above gives the error: ValueError: Extra data: line 2 column 1 - line 2 column 2376 (char 1489 - 3864) How do I read the file with more than one json object?
The issue comes when you write your JSON. You must write a single JSON object, so you may also load a single JSON object. Currently, you are writing multiple separate objects, causing the error. Modify your write code a bit: json_data = [] with open("followers.txt", 'a') as f: for follower in limit_handled(tweepy.Cursor(api.followers, screen_name=account_name).pages()): for user_obj in follower: json_data.append(user_obj._json) # outside the loops json.dump(json_data, f) Now, when reading, your existing code should work. You'll get a list of dictionaries.
Of course it is best to treat the problem from the root: write a single json and read it, as suggested by COLDSPEED. However, if you already wrote multiple json objects into a single file, you can try the following code to use the already created file: import json follower_data = [] # a list of all objects with open('followers.txt') as json_data: for line in json_data: follower_data.append( json.loads(line) ) Assuming you did not indent your json objects when you wrote them to 'flowers.txt', then each line in the file is a json object that can be parsed independantly.
How to add a key-value to JSON data retrieved from a file?
I am new to Python and I am playing with JSON data. I would like to retrieve the JSON data from a file and add to that data a JSON key-value "on the fly". That is, my json_file contains JSON data as-like the following: {"key1": {"key1A": ["value1", "value2"], "key1B": {"key1B1": "value3"}}} I would like to add the "ADDED_KEY": "ADDED_VALUE" key-value part to the above data so to use the following JSON in my script: {"ADDED_KEY": "ADDED_VALUE", "key1": {"key1A": ["value1", "value2"], "key1B": {"key1B1": "value3"}}} I am trying to write something as-like the following in order to accomplish the above: import json json_data = open(json_file) json_decoded = json.load(json_data) # What I have to make here?! json_data.close()
Your json_decoded object is a Python dictionary; you can simply add your key to that, then re-encode and rewrite the file: import json with open(json_file) as json_file: json_decoded = json.load(json_file) json_decoded['ADDED_KEY'] = 'ADDED_VALUE' with open(json_file, 'w') as json_file: json.dump(json_decoded, json_file) I used the open file objects as context managers here (with the with statement) so Python automatically closes the file when done.
Json returned from json.loads() behave just like native python lists/dictionaries: import json with open("your_json_file.txt", 'r') as f: data = json.loads(f.read()) #data becomes a dictionary #do things with data here data['ADDED_KEY'] = 'ADDED_VALUE' #and then just write the data back on the file with open("your_json_file.txt", 'w') as f: f.write(json.dumps(data, sort_keys=True, indent=4, separators=(',', ': '))) #I added some options for pretty printing, play around with them! For more info check out the official doc
You can do json_decoded['ADDED_KEY'] = 'ADDED_VALUE' OR json_decoded.update({"ADDED_KEY":"ADDED_VALUE"}) which works nicely if you want to add more than one key/value pair. Of course, you may want to check for the existence of ADDED_KEY first - depends on your needs. AND I assume you want might want to save that data back to the file json.dump(json_decoded, open(json_file,'w'))