如何将 JSON 文件转换为其他格式

如何将 JSON 文件转换为其他格式

我的文件有 100 行,格式如下。

{ "type": "Feature", "properties": { "id":"01","score":"10000","Name": "ABC", "description": "<html xmlns:fo=\"http:\/\/www.w3.org\/1999\/XSL\/Format\" xmlns:msxsl=\"urn:schemas-microsoft-com:xslt\">\n\n<head>\n\n<META http-equiv=\"Content-Type\" content=\"text\/html\">\n\n<meta http-equiv=\"content-type\" content=\"text\/html; charset=UTF-8\">\n\n<\/head>\n\n<body style=\"margin:0px 0px 0px 0px;overflow:auto;background:#FFFFFF;\">\n\n<table style=\"font-family:Arial,Verdana,Times;font-size:12px;text-align:left;width:100%;border-collapse:collapse;padding:3px 3px 3px 3px\">\n\n<tr style=\"text-align:center;font-weight:bold;background:#9CBCE2\">\n\n<td>PASIR PANJANG 1<\/td>\n\n<\/tr>\n\n<tr>\n\n<td>\n\n<table style=\"font-family:Arial,Verdana,Times;font-size:12px;text-align:left;width:100%;border-spacing:0px; padding:3px 3px 3px 3px\">\n\n<tr>\n\n<td>SHAPE<\/td>\n\n<td>Polygon<\/td>\n\n<\/tr>\n\n<tr bgcolor=\"#D4E4F3\">\n\n<td>Subzone Number<\/td>\n\n<td>13<\/td>\n\n<\/tr>\n\n<tr>\n\n<td>Subzone Name<\/td>\n\n<td>ABC 1<\/td>\n\n<\/tr>\n\n<tr bgcolor=\"#D4E4F3\">\n\n<td>Subzone Code<\/td>\n\n<td>QTSZ13<\/td>\n\n<\/tr>\n\n<tr>\n\n<td>Central Area Indicator<\/td>\n\n<td>N<\/td>\n\n<\/tr>\n\n<tr bgcolor=\"#D4E4F3\">\n\n<td>Planning Area Name<\/td>\n\n<td>QUEENSTOWN<\/td>\n\n<\/tr>\n\n<tr>\n\n<td>Planning Area Code<\/td>\n\n<td>QT<\/td>\n\n<\/tr>\n\n<tr bgcolor=\"#D4E4F3\">\n\n<td>Region Name<\/td>\n\n<td>CENTRAL REGION<\/td>\n\n<\/tr>\n\n<tr>\n\n<td>Region Code<\/td>\n\n<td>CR<\/td>\n\n<\/tr>\n\n<tr bgcolor=\"#D4E4F3\">\n\n<td>INC_CRC<\/td>\n\n<td>1F721290C421BFAB<\/td>\n\n<\/tr>\n\n<tr>\n\n<td>FMEL_UPD_D<\/td>\n\n<td>12\/5\/2014 9:26:20 PM<\/td>\n\n<\/tr>\n\n<tr bgcolor=\"#D4E4F3\">\n\n<td>X_ADDR<\/td>\n\n<td>22077.3383<\/td>\n\n<\/tr>\n\n<tr>\n\n<td>Y_ADDR<\/td>\n\n<td>29893.7812<\/td>\n\n<\/tr>\n\n<tr bgcolor=\"#D4E4F3\">\n\n<td>SHAPE_Length<\/td>\n\n<td>6571.323082<\/td>\n\n<\/tr>\n\n<tr>\n\n<td>SHAPE_Area<\/td>\n\n<td>1084792.320587<\/td>\n\n<\/tr>\n\n<\/table>\n\n<\/td>\n\n<\/tr>\n\n<\/table>\n\n<\/body>\n\n<\/html>\n\n"}, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 103.770389070495796, 1.292487855396017 ], [ 103.770414424305599, 1.292574882101212 ], [ 103.7701466034909, 1.29263278185594 ], [ 103.770073030489101, 1.292648696824341 ], [ 103.769991818022206, 1.292739600582147 ] ] ] ] } },

... .. .. 由此我需要将每一行转换为

01,ABC,"POLYGON((103.770389070495796, 1.292487855396017 ,  103.770414424305599, 1.292574882101212 ,  103.7701466034909, 1.29263278185594 ,  103.770073030489101, 1.292648696824341 ,  103.769991818022206, 1.292739600582147))"

等等。

可以用 awk 或 sed 完成吗?

答案1

要处理 JSON,请使用 JSON 工具,例如杰克(通常可用的是您的发行版中的软件包):

jq -j '.properties.id, ",", .([ .geometry.coordinates | recurse | numbers | tostring] | join(",")), "))\n"' < your_file

做你想要的单行。您可能需要单独输入每一行,具体取决于文件的实际外观。

答案2

另一种解决方案使用jq

jq -j '.properties.id, ",",
    .properties.Name, ",\"POLYGON((",
    (.geometry.coordinates | flatten | map(tostring) | join(",")), "))\"\n"' < file.json

输出:

01,ABC,"POLYGON((103.770389070496,1.29248785539602,103.770414424306,1.29257488210121,103.770146603491,1.29263278185594,103.770073030489,1.29264869682434,103.769991818022,1.29273960058215))"

编辑: 小提琴 1,小提琴2

相关内容