Como converter um arquivo JSON em um formato diferente

0

Estou tendo um arquivo com 100 linhas no formato abaixo.

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

.. .. A partir disso, preciso converter cada linha para

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

e assim por diante.

Pode ser feito em awk ou sed?

    
por John 23.05.2017 / 08:28

2 respostas

2

Outra solução que usa jq :

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

Saída:

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

Editar: violino 1 , violino 2

    
por 23.05.2017 / 16:21
4

Para processar o JSON, use uma ferramenta JSON, por exemplo jq (normalmente disponível em um pacote da sua distro):

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

faz o que você quer para uma única linha. Talvez seja necessário alimentar cada linha separadamente, dependendo de como o arquivo realmente se parece.

    
por 23.05.2017 / 10:09