2

I am working on loading JSON data into Redshift, but for it to work the commas have to be removed between the objects. If I remove the commas then it works fine.

Can someone tell me how to remove the commas between objects so that I can load it into Redshift?

The Redshift copy command allows only arrays of objects to be loaded as rows.

Currently I have this 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
                    }
            }
    }]

I need to transform it to this:

    {
                "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
                            }
                    }
            }
skirtle
  • 27,868
  • 4
  • 42
  • 57
Shruti Purushan
  • 103
  • 1
  • 2
  • 9
  • `Array of objects` needs commas. Problem is likely something else. – Brahma Dev Oct 25 '17 at 09:23
  • There was similar problem but in the opposite direction. Look here: https://stackoverflow.com/questions/38695977/convert-json-object-containing-objects-into-an-array-of-objects – Arkadiusz Oct 25 '17 at 09:25
  • Going by their [documentation](http://docs.aws.amazon.com/redshift/latest/dg/copy-usage_notes-copy-from-json.html) your expected output seems right, but that's not `Array of Objects`. – Brahma Dev Oct 25 '17 at 09:30

2 Answers2

3

You could:

  • json parse your code,
  • loop on rows
  • output each row as json stringified

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);
  • yea i can do that.I am looking for some regex expression to remove it. – Shruti Purushan Oct 25 '17 at 09:34
  • something like this https://stackoverflow.com/questions/39655387/javascript-how-to-add-comma-in-between-two-object-in-string-object-but-not-last – Shruti Purushan Oct 25 '17 at 09:36
  • Easy with regular expressions, only if your data are "safe" (I mean: if your data doesn't contain any "{" or "}"). –  Oct 25 '17 at 09:40
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);

If you already have a string representation. Works as long as there is no JSON strings inside the JSON object.

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);
Brahma Dev
  • 1,955
  • 1
  • 12
  • 18