Write a Dataframe to a JSON File



Resource

Data Source


Import Pandas

# Import Pandas
import pandas as pd

df = pd.read_csv("data/cereal.csv")

Output as a JSON string

# output as a JSON string
df.to_json()
'{"name":{"0":"100% Bran","1":"100% Natural Bran","2":"All-Bran","3":"All-Bran with Extra Fiber","4":"Almond Delight","5":"Apple Cinnamon Cheerios","6":"Apple Jacks","7":"Basic 4","8":"Bran Chex","9":"Bran Flakes","10":"Cap\'n\'Crunch","11":"Cheerios","12":"Cinnamon Toast Crunch","13":"Clusters","14":"Cocoa Puffs","15":"Corn Chex","16":"Corn Flakes","17":"Corn Pops","18":"Count Chocula","19":"Cracklin\' Oat Bran","20":"Cream of Wheat (Quick)","21":"Crispix","22":"Crispy Wheat & Raisins","23":"Double Chex","24":"Froot Loops","25":"Frosted Flakes","26":"Frosted Mini-Wheats","27":"Fruit & Fibre Dates; Walnuts; and Oats","28":"Fruitful Bran","29":"Fruity Pebbles","30":"Golden Crisp","31":"Golden Grahams","32":"Grape Nuts Flakes","33":"Grape-Nuts","34":"Great Grains Pecan","35":"Honey Graham Ohs","36":"Honey Nut Cheerios","37":"Honey-comb","38":"Just Right Crunchy  Nuggets","39":"Just Right Fruit & Nut","40":"Kix","41":"Life","42":"Lucky Charms","43":"Maypo","44":"Muesli Raisins; Dates; & Almonds","45":"Muesli Raisins; Peaches; & Pecans","46":"Mueslix Crispy Blend","47":"Multi-Grain Cheerios","48":"Nut&Honey Crunch","49":"Nutri-Grain Almond-Raisin","50":"Nutri-grain Wheat","51":"Oatmeal Raisin Crisp","52":"Post Nat. Raisin Bran","53":"Product 19","54":"Puffed Rice","55":"Puffed Wheat","56":"Quaker Oat Squares","57":"Quaker Oatmeal","58":"Raisin Bran","59":"Raisin Nut Bran","60":"Raisin Squares","61":"Rice Chex","62":"Rice Krispies","63":"Shredded Wheat","64":"Shredded Wheat \'n\'Bran","65":"Shredded Wheat spoon size","66":"Smacks","67":"Special K","68":"Strawberry Fruit Wheats","69":"Total Corn Flakes","70":"Total Raisin Bran","71":"Total Whole Grain","72":"Triples","73":"Trix","74":"Wheat Chex","75":"Wheaties","76":"Wheaties Honey Gold"},"mfr":{"0":"N","1":"Q","2":"K","3":"K","4":"R","5":"G","6":"K","7":"G","8":"R","9":"P","10":"Q","11":"G","12":"G","13":"G","14":"G","15":"R","16":"K","17":"K","18":"G","19":"K","20":"N","21":"K","22":"G","23":"R","24":"K","25":"K","26":"K","27":"P","28":"K","29":"P","30":"P","31":"G","32":"P","33":"P","34":"P","35":"Q","36":"G","37":"P","38":"K","39":"K","40":"G","41":"Q","42":"G","43":"A","44":"R","45":"R","46":"K","47":"G","48":"K","49":"K","50":"K","51":"G","52":"P","53":"K","54":"Q","55":"Q","56":"Q","57":"Q","58":"K","59":"G","60":"K","61":"R","62":"K","63":"N","64":"N","65":"N","66":"K","67":"K","68":"N","69":"G","70":"G","71":"G","72":"G","73":"G","74":"R","75":"G","76":"G"},"type":{"0":"C","1":"C","2":"C","3":"C","4":"C","5":"C","6":"C","7":"C","8":"C","9":"C","10":"C","11":"C","12":"C","13":"C","14":"C","15":"C","16":"C","17":"C","18":"C","19":"C","20":"H","21":"C","22":"C","23":"C","24":"C","25":"C","26":"C","27":"C","28":"C","29":"C","30":"C","31":"C","32":"C","33":"C","34":"C","35":"C","36":"C","37":"C","38":"C","39":"C","40":"C","41":"C","42":"C","43":"H","44":"C","45":"C","46":"C","47":"C","48":"C","49":"C","50":"C","51":"C","52":"C","53":"C","54":"C","55":"C","56":"C","57":"H","58":"C","59":"C","60":"C","61":"C","62":"C","63":"C","64":"C","65":"C","66":"C","67":"C","68":"C","69":"C","70":"C","71":"C","72":"C","73":"C","74":"C","75":"C","76":"C"},"calories":{"0":70,"1":120,"2":70,"3":50,"4":110,"5":110,"6":110,"7":130,"8":90,"9":90,"10":120,"11":110,"12":120,"13":110,"14":110,"15":110,"16":100,"17":110,"18":110,"19":110,"20":100,"21":110,"22":100,"23":100,"24":110,"25":110,"26":100,"27":120,"28":120,"29":110,"30":100,"31":110,"32":100,"33":110,"34":120,"35":120,"36":110,"37":110,"38":110,"39":140,"40":110,"41":100,"42":110,"43":100,"44":150,"45":150,"46":160,"47":100,"48":120,"49":140,"50":90,"51":130,"52":120,"53":100,"54":50,"55":50,"56":100,"57":100,"58":120,"59":100,"60":90,"61":110,"62":110,"63":80,"64":90,"65":90,"66":110,"67":110,"68":90,"69":110,"70":140,"71":100,"72":110,"73":110,"74":100,"75":100,"76":110},"protein":{"0":4,"1":3,"2":4,"3":4,"4":2,"5":2,"6":2,"7":3,"8":2,"9":3,"10":1,"11":6,"12":1,"13":3,"14":1,"15":2,"16":2,"17":1,"18":1,"19":3,"20":3,"21":2,"22":2,"23":2,"24":2,"25":1,"26":3,"27":3,"28":3,"29":1,"30":2,"31":1,"32":3,"33":3,"34":3,"35":1,"36":3,"37":1,"38":2,"39":3,"40":2,"41":4,"42":2,"43":4,"44":4,"45":4,"46":3,"47":2,"48":2,"49":3,"50":3,"51":3,"52":3,"53":3,"54":1,"55":2,"56":4,"57":5,"58":3,"59":3,"60":2,"61":1,"62":2,"63":2,"64":3,"65":3,"66":2,"67":6,"68":2,"69":2,"70":3,"71":3,"72":2,"73":1,"74":3,"75":3,"76":2},"fat":{"0":1,"1":5,"2":1,"3":0,"4":2,"5":2,"6":0,"7":2,"8":1,"9":0,"10":2,"11":2,"12":3,"13":2,"14":1,"15":0,"16":0,"17":0,"18":1,"19":3,"20":0,"21":0,"22":1,"23":0,"24":1,"25":0,"26":0,"27":2,"28":0,"29":1,"30":0,"31":1,"32":1,"33":0,"34":3,"35":2,"36":1,"37":0,"38":1,"39":1,"40":1,"41":2,"42":1,"43":1,"44":3,"45":3,"46":2,"47":1,"48":1,"49":2,"50":0,"51":2,"52":1,"53":0,"54":0,"55":0,"56":1,"57":2,"58":1,"59":2,"60":0,"61":0,"62":0,"63":0,"64":0,"65":0,"66":1,"67":0,"68":0,"69":1,"70":1,"71":1,"72":1,"73":1,"74":1,"75":1,"76":1},"sodium":{"0":130,"1":15,"2":260,"3":140,"4":200,"5":180,"6":125,"7":210,"8":200,"9":210,"10":220,"11":290,"12":210,"13":140,"14":180,"15":280,"16":290,"17":90,"18":180,"19":140,"20":80,"21":220,"22":140,"23":190,"24":125,"25":200,"26":0,"27":160,"28":240,"29":135,"30":45,"31":280,"32":140,"33":170,"34":75,"35":220,"36":250,"37":180,"38":170,"39":170,"40":260,"41":150,"42":180,"43":0,"44":95,"45":150,"46":150,"47":220,"48":190,"49":220,"50":170,"51":170,"52":200,"53":320,"54":0,"55":0,"56":135,"57":0,"58":210,"59":140,"60":0,"61":240,"62":290,"63":0,"64":0,"65":0,"66":70,"67":230,"68":15,"69":200,"70":190,"71":200,"72":250,"73":140,"74":230,"75":200,"76":200},"fiber":{"0":10.0,"1":2.0,"2":9.0,"3":14.0,"4":1.0,"5":1.5,"6":1.0,"7":2.0,"8":4.0,"9":5.0,"10":0.0,"11":2.0,"12":0.0,"13":2.0,"14":0.0,"15":0.0,"16":1.0,"17":1.0,"18":0.0,"19":4.0,"20":1.0,"21":1.0,"22":2.0,"23":1.0,"24":1.0,"25":1.0,"26":3.0,"27":5.0,"28":5.0,"29":0.0,"30":0.0,"31":0.0,"32":3.0,"33":3.0,"34":3.0,"35":1.0,"36":1.5,"37":0.0,"38":1.0,"39":2.0,"40":0.0,"41":2.0,"42":0.0,"43":0.0,"44":3.0,"45":3.0,"46":3.0,"47":2.0,"48":0.0,"49":3.0,"50":3.0,"51":1.5,"52":6.0,"53":1.0,"54":0.0,"55":1.0,"56":2.0,"57":2.7,"58":5.0,"59":2.5,"60":2.0,"61":0.0,"62":0.0,"63":3.0,"64":4.0,"65":3.0,"66":1.0,"67":1.0,"68":3.0,"69":0.0,"70":4.0,"71":3.0,"72":0.0,"73":0.0,"74":3.0,"75":3.0,"76":1.0},"carbo":{"0":5.0,"1":8.0,"2":7.0,"3":8.0,"4":14.0,"5":10.5,"6":11.0,"7":18.0,"8":15.0,"9":13.0,"10":12.0,"11":17.0,"12":13.0,"13":13.0,"14":12.0,"15":22.0,"16":21.0,"17":13.0,"18":12.0,"19":10.0,"20":21.0,"21":21.0,"22":11.0,"23":18.0,"24":11.0,"25":14.0,"26":14.0,"27":12.0,"28":14.0,"29":13.0,"30":11.0,"31":15.0,"32":15.0,"33":17.0,"34":13.0,"35":12.0,"36":11.5,"37":14.0,"38":17.0,"39":20.0,"40":21.0,"41":12.0,"42":12.0,"43":16.0,"44":16.0,"45":16.0,"46":17.0,"47":15.0,"48":15.0,"49":21.0,"50":18.0,"51":13.5,"52":11.0,"53":20.0,"54":13.0,"55":10.0,"56":14.0,"57":-1.0,"58":14.0,"59":10.5,"60":15.0,"61":23.0,"62":22.0,"63":16.0,"64":19.0,"65":20.0,"66":9.0,"67":16.0,"68":15.0,"69":21.0,"70":15.0,"71":16.0,"72":21.0,"73":13.0,"74":17.0,"75":17.0,"76":16.0},"sugars":{"0":6,"1":8,"2":5,"3":0,"4":8,"5":10,"6":14,"7":8,"8":6,"9":5,"10":12,"11":1,"12":9,"13":7,"14":13,"15":3,"16":2,"17":12,"18":13,"19":7,"20":0,"21":3,"22":10,"23":5,"24":13,"25":11,"26":7,"27":10,"28":12,"29":12,"30":15,"31":9,"32":5,"33":3,"34":4,"35":11,"36":10,"37":11,"38":6,"39":9,"40":3,"41":6,"42":12,"43":3,"44":11,"45":11,"46":13,"47":6,"48":9,"49":7,"50":2,"51":10,"52":14,"53":3,"54":0,"55":0,"56":6,"57":-1,"58":12,"59":8,"60":6,"61":2,"62":3,"63":0,"64":0,"65":0,"66":15,"67":3,"68":5,"69":3,"70":14,"71":3,"72":3,"73":12,"74":3,"75":3,"76":8},"potass":{"0":280,"1":135,"2":320,"3":330,"4":-1,"5":70,"6":30,"7":100,"8":125,"9":190,"10":35,"11":105,"12":45,"13":105,"14":55,"15":25,"16":35,"17":20,"18":65,"19":160,"20":-1,"21":30,"22":120,"23":80,"24":30,"25":25,"26":100,"27":200,"28":190,"29":25,"30":40,"31":45,"32":85,"33":90,"34":100,"35":45,"36":90,"37":35,"38":60,"39":95,"40":40,"41":95,"42":55,"43":95,"44":170,"45":170,"46":160,"47":90,"48":40,"49":130,"50":90,"51":120,"52":260,"53":45,"54":15,"55":50,"56":110,"57":110,"58":240,"59":140,"60":110,"61":30,"62":35,"63":95,"64":140,"65":120,"66":40,"67":55,"68":90,"69":35,"70":230,"71":110,"72":60,"73":25,"74":115,"75":110,"76":60},"vitamins":{"0":25,"1":0,"2":25,"3":25,"4":25,"5":25,"6":25,"7":25,"8":25,"9":25,"10":25,"11":25,"12":25,"13":25,"14":25,"15":25,"16":25,"17":25,"18":25,"19":25,"20":0,"21":25,"22":25,"23":25,"24":25,"25":25,"26":25,"27":25,"28":25,"29":25,"30":25,"31":25,"32":25,"33":25,"34":25,"35":25,"36":25,"37":25,"38":100,"39":100,"40":25,"41":25,"42":25,"43":25,"44":25,"45":25,"46":25,"47":25,"48":25,"49":25,"50":25,"51":25,"52":25,"53":100,"54":0,"55":0,"56":25,"57":0,"58":25,"59":25,"60":25,"61":25,"62":25,"63":0,"64":0,"65":0,"66":25,"67":25,"68":25,"69":100,"70":100,"71":100,"72":25,"73":25,"74":25,"75":25,"76":25},"shelf":{"0":3,"1":3,"2":3,"3":3,"4":3,"5":1,"6":2,"7":3,"8":1,"9":3,"10":2,"11":1,"12":2,"13":3,"14":2,"15":1,"16":1,"17":2,"18":2,"19":3,"20":2,"21":3,"22":3,"23":3,"24":2,"25":1,"26":2,"27":3,"28":3,"29":2,"30":1,"31":2,"32":3,"33":3,"34":3,"35":2,"36":1,"37":1,"38":3,"39":3,"40":2,"41":2,"42":2,"43":2,"44":3,"45":3,"46":3,"47":1,"48":2,"49":3,"50":3,"51":3,"52":3,"53":3,"54":3,"55":3,"56":3,"57":1,"58":2,"59":3,"60":3,"61":1,"62":1,"63":1,"64":1,"65":1,"66":2,"67":1,"68":2,"69":3,"70":3,"71":3,"72":3,"73":2,"74":1,"75":1,"76":1},"weight":{"0":1.0,"1":1.0,"2":1.0,"3":1.0,"4":1.0,"5":1.0,"6":1.0,"7":1.33,"8":1.0,"9":1.0,"10":1.0,"11":1.0,"12":1.0,"13":1.0,"14":1.0,"15":1.0,"16":1.0,"17":1.0,"18":1.0,"19":1.0,"20":1.0,"21":1.0,"22":1.0,"23":1.0,"24":1.0,"25":1.0,"26":1.0,"27":1.25,"28":1.33,"29":1.0,"30":1.0,"31":1.0,"32":1.0,"33":1.0,"34":1.0,"35":1.0,"36":1.0,"37":1.0,"38":1.0,"39":1.3,"40":1.0,"41":1.0,"42":1.0,"43":1.0,"44":1.0,"45":1.0,"46":1.5,"47":1.0,"48":1.0,"49":1.33,"50":1.0,"51":1.25,"52":1.33,"53":1.0,"54":0.5,"55":0.5,"56":1.0,"57":1.0,"58":1.33,"59":1.0,"60":1.0,"61":1.0,"62":1.0,"63":0.83,"64":1.0,"65":1.0,"66":1.0,"67":1.0,"68":1.0,"69":1.0,"70":1.5,"71":1.0,"72":1.0,"73":1.0,"74":1.0,"75":1.0,"76":1.0},"cups":{"0":0.33,"1":1.0,"2":0.33,"3":0.5,"4":0.75,"5":0.75,"6":1.0,"7":0.75,"8":0.67,"9":0.67,"10":0.75,"11":1.25,"12":0.75,"13":0.5,"14":1.0,"15":1.0,"16":1.0,"17":1.0,"18":1.0,"19":0.5,"20":1.0,"21":1.0,"22":0.75,"23":0.75,"24":1.0,"25":0.75,"26":0.8,"27":0.67,"28":0.67,"29":0.75,"30":0.88,"31":0.75,"32":0.88,"33":0.25,"34":0.33,"35":1.0,"36":0.75,"37":1.33,"38":1.0,"39":0.75,"40":1.5,"41":0.67,"42":1.0,"43":1.0,"44":1.0,"45":1.0,"46":0.67,"47":1.0,"48":0.67,"49":0.67,"50":1.0,"51":0.5,"52":0.67,"53":1.0,"54":1.0,"55":1.0,"56":0.5,"57":0.67,"58":0.75,"59":0.5,"60":0.5,"61":1.13,"62":1.0,"63":1.0,"64":0.67,"65":0.67,"66":0.75,"67":1.0,"68":1.0,"69":1.0,"70":1.0,"71":1.0,"72":0.75,"73":1.0,"74":0.67,"75":1.0,"76":0.75},"rating":{"0":68.402973,"1":33.983679,"2":59.425505,"3":93.704912,"4":34.384843,"5":29.509541,"6":33.174094,"7":37.038562,"8":49.120253,"9":53.313813,"10":18.042851,"11":50.764999,"12":19.823573,"13":40.400208,"14":22.736446,"15":41.445019,"16":45.863324,"17":35.782791,"18":22.396513,"19":40.448772,"20":64.533816,"21":46.895644,"22":36.176196,"23":44.330856,"24":32.207582,"25":31.435973,"26":58.345141,"27":40.917047,"28":41.015492,"29":28.025765,"30":35.252444,"31":23.804043,"32":52.076897,"33":53.371007,"34":45.811716,"35":21.871292,"36":31.072217,"37":28.742414,"38":36.523683,"39":36.471512,"40":39.241114,"41":45.328074,"42":26.734515,"43":54.850917,"44":37.136863,"45":34.139765,"46":30.313351,"47":40.105965,"48":29.924285,"49":40.69232,"50":59.642837,"51":30.450843,"52":37.840594,"53":41.50354,"54":60.756112,"55":63.005645,"56":49.511874,"57":50.828392,"58":39.259197,"59":39.7034,"60":55.333142,"61":41.998933,"62":40.560159,"63":68.235885,"64":74.472949,"65":72.801787,"66":31.230054,"67":53.131324,"68":59.363993,"69":38.839746,"70":28.592785,"71":46.658844,"72":39.106174,"73":27.753301,"74":49.787445,"75":51.592193,"76":36.187559}}'

Output to a JSON File

# Output to a JSON file
# Orient - Allowed values are: {‘split’, ‘records’, ‘index’, ‘table’}.
# Indent - Length of whitespace used to indent each record.
df.to_json('data/new_json.json', orient="split", indent=4)