簡體   English   中英

使用awk或nawk將文本輸入json

[英]text into json using awk or nawk

我有以下文本文件要處理

 **parent**
 father = erik
 mother = rita
 *son*
 name = john
 age = 13
 *daughter*
 name = lili
 age = 24
 status = student

 **parent**
 father = boby
 mother = christa
 *son*
 name = tim
 age = 2

 **parent**
 father = leo
 mother = victoria
 *daughter*
 name = kim
 age = 36
 occupation = singer
 haircolor = blond

並且需要具有以下JSON格式:

{"parent": [
             { "father": "erik",
               "mother": "rita", 
               "son": {
                   "name": "john",
                   "age": "13"
               },
               "daughter": {
                   "name": "lili",
                   "age": "24",
                   "occupation": "student"
               }
             },
             { "father": "boby",
               "mother": "christa",
               "son": {
                   "name": "tim",
                   "age": "2"
               }
             },
             { "father": "leo",
               "mother": "victoria",
               "daughter": {
                   "name": "kim",
                   "age": "36",
                   "occupation": "singer",
                   "haircolor": "blond"
               }
             }
            ]
  }

我的問題是如何用nawk或awk編寫代碼來做到這一點。 要考慮的要點:

  • 不是每個父母(父親和母親)兒子或女兒都存在
  • 兒子或女兒可能具有或沒有不同的參數,這些參數在其他孩子中不存在,即職業,體重,染發

我會改用perl之類的語言,在其中我可以用本地語言建立數據結構,然后將其編碼為JSON

perl -MJSON -ne '
  BEGIN {$root = {parent=>[]}}
  if (/^[*][*]parent/) {$unit = "family"; $family = {}; next;}
  if (/^[*]son/)       {$unit = "son"; $son = {}; next;}
  if (/^[*]daughter/)  {$unit = "daughter"; $daughter = {}; next;}
  if (/(\w+)\s*=\s*(\w+)/) {${$unit}->{$1} = $2;}
  sub add_family {
    $family->{son} = $son if $son; 
    $family->{daughter} = $daughter if $daughter;
    push @{$root->{parent}}, $family; 
    undef $son; 
    undef $daughter;
    undef $family;
  }
  if (/^$/) {add_family}
  END {
    add_family if $family;
    print to_json($root, {pretty=>1}), "\n";
  }
' file
{
   "parent" : [
      {
         "son" : {
            "name" : "john",
            "age" : "13"
         },
         "daughter" : {
            "name" : "lili",
            "status" : "student",
            "age" : "24"
         },
         "father" : "erik",
         "mother" : "rita"
      },
      {
         "son" : {
            "name" : "tim",
            "age" : "2"
         },
         "father" : "boby",
         "mother" : "christa"
      },
      {
         "father" : "leo",
         "daughter" : {
            "age" : "36",
            "occupation" : "singer",
            "name" : "kim",
            "haircolor" : "blond"
         },
         "mother" : "victoria"
      }
   ]
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM