Reformat JSON file?
<p>I have two JSON files.</p>

<p>File A:</p>

<pre><code>  "features": [
  {
   "attributes": {
    "NAME": "R T CO",
    "LTYPE": 64,
    "QUAD15M": "279933",
    "OBJECTID": 225,
    "SHAPE.LEN": 828.21510830520401
   },
   "geometry": {
    "paths": [
     [
      [
       -99.818614674337155,
       27.782542677671653
      ],
      [
       -99.816056346719051,
       27.782590806976135
      ]
     ]
    ]
   }
  }
</code></pre>

<p>File B:</p>

<pre><code>  "features": [
{
  "geometry": {
    "type": "MultiLineString",  
    "coordinates": [
      [
        [
          -99.773315512624,
          27.808875128096
        ],
        [
          -99.771397939251,
          27.809512259374
        ]
      ]
    ]
  },
  "type": "Feature",
  "properties": {
    "LTYPE": 64,
    "SHAPE.LEN": 662.3800009247,
    "NAME": "1586",
    "OBJECTID": 204,
    "QUAD15M": "279933"
  }
},
</code></pre>

<p>I would like File B to be reformatted to look like File A.
Change "properties" to "attributes", "coordinates" to "paths", and remove both "type": "MultiLineString" and "type": "Feature". What is the best way to do this via python?</p>

<p>Is there a way to also reorder the "attributes" key value pairs to look like File A? </p>

<p>It's a rather large dataset and I would like to iterate through the entire file.</p>

<p>What about:</p>

<pre><code>cat &lt;file&gt; | python -m json.tool
</code></pre>

<p>This will reformat the contents of the file into a uniform human readable format. If you really need to change the names of the fields you could use sed.</p>

<pre><code>cat &lt;file&gt; | sed -e 's/"properties"/"attributes"/'
</code></pre>

<p>This may be sufficient for your use case. If you have something that requires more nuanced parsing, you'll have to read up on how to manage the JSON through an ORM library.</p>

python json reformatting