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>Manipulating JSON in Python is a good candidate for the <a href="https://en.wikipedia.org/wiki/IPO_model" rel="nofollow">input-process-output model</a> of programming.</p>

<p>For input, you convert the external JSON file into a Python data structure, using <a href="https://docs.python.org/2/library/json.html#json.load" rel="nofollow"><code>json.load()</code></a>.</p>

<p>For output, you convert the Python data structure into an external JSON file using <a href="https://docs.python.org/2/library/json.html#json.dump" rel="nofollow"><code>json.dump()</code></a>.</p>

<p>For the processing or conversion step, do whatever it is that you need to do, using ordinary Python <code>dict</code> and <code>list</code> methods.</p>

<p>This program might do what you want:</p>

<pre><code>import json

with open("b.json") as b:
    b = json.load(b)

for feature in b["features"]:

    feature["attributes"] = feature["properties"]
    del feature["properties"]

    feature["geometry"]["paths"] = feature["geometry"]["coordinates"]
    del feature["geometry"]["coordinates"]

    del feature["geometry"]["type"]

    del feature["type"]

with open("new-b.json", "w") as new_b:
    json.dump(b, new_b, indent=1, separators=(',', ': '))
</code></pre>

python json reformatting