2017-10-25 9 views
1

JSONデータをRedshiftに読み込む作業をしていますが、動作させるにはオブジェクト間でコンマを削除する必要があります。カンマを削除しても正常に動作します。JSONアレイ内のオブジェクト間のコンマを削除する

Redshiftに読み込むことができるようにオブジェクト間のカンマを削除する方法を教えてもらえますか?

Redshift copyコマンドでは、オブジェクトの配列だけを行としてロードできます。

現在、私はこのJSONを持っている:

[{ 
     "id":"57e4d12e53a5a", 
     "body":"asdas", 
     "published":"Fri, 
     23 Sep 2016 06:52:30 +0000", 
     "type":"chat-message", 
     "actor": 
      { 
      "displayName":"beau", 
      "objectType":"person", 
      "image": 
       { 
       "url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g", 
       "width":48,"height":48 
       } 
      } 
    }, 
    { 
     "id":"57e4d51165d97", 
     "body":"jackiechanSADAS", 
     "published":"Fri, 23 Sep 2016 07:09:05 +0000", 
     "type":"chat-message", 
     "actor": 
      { 
       "displayName":"beau", 
       "objectType":"person", 
       "image": 
        { 
         "url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g", 
         "width":48, 
         "height":48 
        } 
      } 
    }, 
    { 
     "id":"asas", 
     "body":"peterting", 
     "published":"Fri, 23 Sep 2016 07:09:05 +0000", 
     "type":"chat-message", 
     "actor": 
      { 
       "displayName":"beau", 
       "objectType":"person", 
       "image": 
        { 
         "url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g", 
         "width":48, 
         "height":48 
        } 
      } 
    }] 

私はこれにそれを変換する必要があります。

{ 
       "id":"57e4d12e53a5a", 
       "body":"asdas", 
       "published":"Fri, 
       23 Sep 2016 06:52:30 +0000", 
       "type":"chat-message", 
       "actor": 
        { 
        "displayName":"beau", 
        "objectType":"person", 
        "image": 
         { 
         "url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g", 
         "width":48,"height":48 
         } 
        } 
      } 
      { 
       "id":"57e4d51165d97", 
       "body":"jackiechanSADAS", 
       "published":"Fri, 23 Sep 2016 07:09:05 +0000", 
       "type":"chat-message", 
       "actor": 
        { 
         "displayName":"beau", 
         "objectType":"person", 
         "image": 
          { 
           "url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g", 
           "width":48, 
           "height":48 
          } 
        } 
      } 
      { 
       "id":"asas", 
       "body":"peterting", 
       "published":"Fri, 23 Sep 2016 07:09:05 +0000", 
       "type":"chat-message", 
       "actor": 
        { 
         "displayName":"beau", 
         "objectType":"person", 
         "image": 
          { 
           "url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g", 
           "width":48, 
           "height":48 
          } 
        } 
      } 
+0

カンマが必要objects'の '配列。問題はおそらく他にあります。 –

+0

同様の問題 がありましたが、反対方向です。ここをクリックしてください:https://stackoverflow.com/questions/38695977/convert-json-object-containing-objects-into-an-array-of-objects – Arkadiusz

+0

[documentation](http://docs.aws。 amazon.com/redshift/latest/dg/copy-usage_notes-copy-from-json.html)あなたの期待される出力は正しいようですが、それは 'Array of Objects'ではありません。 –

答えて

2

あなたができます

  • JSONは、
  • ループをあなたのコードを解析行の列
  • 出力各行JSONは、AS

を文字列化として:あなた場合

// Your Array as String 
 

 
let myArray_as_string = ` 
 
[ 
 
    { 
 
    "a": "first", "objet": "with commas" 
 
    }, 
 
    { 
 
    "an": "other", "objet": "2" 
 
    }, 
 
    { 
 
    "a": "third", "objet": "3" 
 
    } 
 
] 
 
`; 
 

 
// JSON parse the string to get a JS Object 
 

 
let myArray_as_object = JSON.parse(myArray_as_string); 
 

 

 
// Make a string with each stringified row 
 

 
let myArray_without_commas = myArray_as_object.map(row => { 
 
    
 
    return JSON.stringify(row); 
 
    
 
}).join("\n") 
 

 
// Do something with the result value 
 

 
console.log(myArray_without_commas);

+0

いや私はそれを行うことができます。私はそれを削除する正規表現を探しています。 –

+0

このようなものhttps://stackoverflow.com/questions/39655387/javascript-how-to-add-comma-in-between-two-object-in-string-object-but-not-last –

+0

簡単な正規表現あなたのデータが「安全」である場合(つまり、データに「{」または「}」が含まれていない場合のみ)。 –

1

let data = [{ 
 
     "id":"57e4d12e53a5a", 
 
     "body":"asdas", 
 
     "published":"Fri, 23 Sep 2016 06:52:30 +0000", 
 
     "type":"chat-message", 
 
     "actor": 
 
      { 
 
      "displayName":"beau", 
 
      "objectType":"person", 
 
      "image": 
 
       { 
 
       "url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g", 
 
       "width":48, 
 
"height":48 
 
       } 
 
      } 
 
    }, 
 
    { 
 
     "id":"57e4d51165d97", 
 
     "body":"jackiechanSADAS", 
 
     "published":"Fri, 23 Sep 2016 07:09:05 +0000", 
 
     "type":"chat-message", 
 
     "actor": 
 
      { 
 
       "displayName":"beau", 
 
       "objectType":"person", 
 
       "image": 
 
        { 
 
         "url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g", 
 
         "width":48, 
 
         "height":48 
 
        } 
 
      } 
 
    }, 
 
    { 
 
     "id":"asas", 
 
     "body":"peterting", 
 
     "published":"Fri, 23 Sep 2016 07:09:05 +0000", 
 
     "type":"chat-message", 
 
     "actor": 
 
      { 
 
       "displayName":"beau", 
 
       "objectType":"person", 
 
       "image": 
 
        { 
 
         "url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g", 
 
         "width":48, 
 
         "height":48 
 
        } 
 
      } 
 
    }] 
 
    
 
    output = data.map(function(e){return JSON.stringify(e,null,2)}).join("\n") 
 
    console.log(output);


既に文字列表現を持っています。 JSONオブジェクト内にJSON文字列がない限り動作します。

let data = JSON.stringify([{ 
 
     "id":"57e4d12e53a5a", 
 
     "body":"asdas", 
 
     "published":"Fri, 23 Sep 2016 06:52:30 +0000", 
 
     "type":"chat-message", 
 
     "actor": 
 
      { 
 
      "displayName":"beau", 
 
      "objectType":"person", 
 
      "image": 
 
       { 
 
       "url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g", 
 
       "width":48, 
 
"height":48 
 
       } 
 
      } 
 
    }, 
 
    { 
 
     "id":"57e4d51165d97", 
 
     "body":"jackiechanSADAS", 
 
     "published":"Fri, 23 Sep 2016 07:09:05 +0000", 
 
     "type":"chat-message", 
 
     "actor": 
 
      { 
 
       "displayName":"beau", 
 
       "objectType":"person", 
 
       "image": 
 
        { 
 
         "url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g", 
 
         "width":48, 
 
         "height":48 
 
        } 
 
      } 
 
    }, 
 
    { 
 
     "id":"asas", 
 
     "body":"peterting", 
 
     "published":"Fri, 23 Sep 2016 07:09:05 +0000", 
 
     "type":"chat-message", 
 
     "actor": 
 
      { 
 
       "displayName":"beau", 
 
       "objectType":"person", 
 
       "image": 
 
        { 
 
         "url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g", 
 
         "width":48, 
 
         "height":48 
 
        } 
 
      } 
 
    }],null,2); 
 
    
 
    output = data.replace(/^\[/,"").replace(/]$/,"").replace(/}\,[\s]+{/g,"}\n\n{") 
 
    console.log(output);

関連する問題