简体   繁体   English

重新格式化JSON文件?

[英]Reformat JSON file?

I have two JSON files. 我有两个JSON文件。

File A: 档案A:

  "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
      ]
     ]
    ]
   }
  }

File B: 档案B:

  "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"
  }
},

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". 我希望将文件B重新格式化为类似于文件A。将“属性”更改为“属性”,将“坐标”更改为“路径”,并同时删除“类型”:“ MultiLineString”和“类型”:“功能”。 What is the best way to do this via python? 通过python做到这一点的最好方法是什么?

Is there a way to also reorder the "attributes" key value pairs to look like File A? 有没有办法对“属性”键值对重新排序以使其看起来像文件A?

It's a rather large dataset and I would like to iterate through the entire file. 这是一个相当大的数据集,我想遍历整个文件。

Manipulating JSON in Python is a good candidate for the input-process-output model of programming. 在Python中处理JSON是编程的输入过程输出模型的不错选择。

For input, you convert the external JSON file into a Python data structure, using json.load() . 作为输入,您可以使用json.load()将外部JSON文件转换为Python数据结构。

For output, you convert the Python data structure into an external JSON file using json.dump() . 为了输出,您可以使用json.dump()将Python数据结构转换为外部JSON文件。

For the processing or conversion step, do whatever it is that you need to do, using ordinary Python dict and list methods. 对于处理或转换步骤,请使用普通的Python dictlist方法执行所需的任何操作。

This program might do what you want: 该程序可能会执行您想要的操作:

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=(',', ': '))

What about: 关于什么:

cat <file> | python -m json.tool

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. 如果确实需要更改字段名称,则可以使用sed。

cat <file> | sed -e 's/"properties"/"attributes"/'

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. 如果您需要更细致的解析,则必须阅读如何通过ORM库管理JSON的知识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM