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
                            }
                    }
            }
3
  • Array of objects needs commas. Problem is likely something else. Commented Oct 25, 2017 at 9:23
  • There was similar problem but in the opposite direction. Look here: stackoverflow.com/questions/38695977/… Commented Oct 25, 2017 at 9:25
  • Going by their documentation your expected output seems right, but that's not Array of Objects. Commented Oct 25, 2017 at 9:30

2 Answers 2

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);

Sign up to request clarification or add additional context in comments.

3 Comments

yea i can do that.I am looking for some regex expression to remove it.
Easy with regular expressions, only if your data are "safe" (I mean: if your data doesn't contain any "{" or "}").
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.