How can I generate a json file in pandas without including the index?

Viewed 38

I have an input json file that looks roughly like this

[
  {
    "identifier": "116S5RJ63",
    "containers": [
      {
        "contains": "soap",
        "height": {
          "unit": "FT",
          "value": 12.07123829231181
        },
        "length": {
          "unit": "FT",
          "value": 12.07123829231181
        },
        "quantity": 1,
        "weight": {
          "unit": "volumeUnits",
          "value": 10000
        },
        "width": {
          "unit": "FT",
          "value": 12.07123829231181
        }
      }
    ],{...}]

I read it in using

input_json  = pd.read_json(input_json_file)

I then process the input_json a bit: nothing dramatic, just changing the contents of some fields. Next I try to output the json again as

input_json.to_json(output_file, orient='records', date_format='iso')

but the output looks like this

[
  {
    "index": 28741,
    "identifier": "115JKLJVZ",
     "containers": [
      {
        "contains": "soap",
        "height": {
          "unit": "FT",
          "value": 12.07123829231181
        },
        "length": {
          "unit": "FT",
          "value": 12.07123829231181
        },
        "quantity": 1,
        "weight": {
          "unit": "volumeUnits",
          "value": 10000
        },
        "width": {
          "unit": "FT",
          "value": 12.07123829231181
        }
      }
    ],{...}]

Specifically it now includes the field 'index', which I thought the orient='records' was supposed to deal with. I'm not sure what to do next. Any suggestions?

1 Answers

Try input_json.reset_index(drop=True, inplace=True) before saving it to file; this should drop the old index from being added as a column. reset_index documentation

Related