2

I have a json code as below:

[  
   [  
      {  
         "title":"Shirt Front",
         "thumbnail":"http://ipadd/pub/media/catalog/product//b/l/blue-back_2.jpg",
         "elements":[  
            {  
               "type":"image",
               "source":"http://ipadd/pub/media/catalog/product//b/l/blue-back_2.jpg",
               "title":"Base",
               "parameters":{  
                  "left":325,
                  "top":329,
                  "colors":"#d59211",
                  "price":20,
                  "colorLinkGroup":"Base",
                  "fill":false
               }
            }
         ]
      },
      {  
         "title":"Shirt Front",
         "thumbnail":"http://ipadd/pub/media/catalog/product//b/l/blue-back_1_1.jpg",
         "elements":[  
            {  
               "type":"image",
               "source":"ipadd/pub/media/catalog/product//b/l/blue-back_1_1.jpg",
               "title":"Base",
               "parameters":{  
                  "left":325,
                  "top":329,
                  "colors":"#d59211",
                  "price":20,
                  "colorLinkGroup":"Base",
                  "fill":false
               }
            }
         ]
      },

   ]
]

I need to remove the comma at the end of array ie. the comma before the last two characters of the json. Due to this the above json is a invalid json. I need to achieve this using JS.

2
  • Is this a string, or JS source code? You could eval it and re-encode it with JSON.stringify. Commented Aug 25, 2019 at 18:48
  • Remove it at the point it's generated. Doing this in JS after receiving the JSON is a hacky workaround. Commented Aug 25, 2019 at 18:50

4 Answers 4

2

Here is another approach, assuming your input is a string. It finds the last index of a comma in the string, and then uses string.splice to assemble a new string without that comma.

const JSON = `[  
   [  
      {  
         "title":"Shirt Front",
         "thumbnail":"http://ipadd/pub/media/catalog/product//b/l/blue-back_2.jpg",
         "elements":[  
            {  
               "type":"image",
               "source":"http://ipadd/pub/media/catalog/product//b/l/blue-back_2.jpg",
               "title":"Base",
               "parameters":{  
                  "left":325,
                  "top":329,
                  "colors":"#d59211",
                  "price":20,
                  "colorLinkGroup":"Base",
                  "fill":false
               }
            }
         ]
      },
      {  
         "title":"Shirt Front",
         "thumbnail":"http://ipadd/pub/media/catalog/product//b/l/blue-back_1_1.jpg",
         "elements":[  
            {  
               "type":"image",
               "source":"ipadd/pub/media/catalog/product//b/l/blue-back_1_1.jpg",
               "title":"Base",
               "parameters":{  
                  "left":325,
                  "top":329,
                  "colors":"#d59211",
                  "price":20,
                  "colorLinkGroup":"Base",
                  "fill":false
               }
            }
         ]
      },

   ]
]`

lastComma = JSON.lastIndexOf(',')

const JSONnew = JSON.slice(0, lastComma) + JSON.slice(lastComma + 1, JSON.length)

console.log(JSONnew)

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

Comments

1

You can use eval, but know that it is dangerous, this code is valid JavaScript

const json = `[  
   [  
      {  
         "title":"Shirt Front",
         "thumbnail":"http://ipadd/pub/media/catalog/product//b/l/blue-back_2.jpg",
         "elements":[  
            {  
               "type":"image",
               "source":"http://ipadd/pub/media/catalog/product//b/l/blue-back_2.jpg",
               "title":"Base",
               "parameters":{  
                  "left":325,
                  "top":329,
                  "colors":"#d59211",
                  "price":20,
                  "colorLinkGroup":"Base",
                  "fill":false
               }
            }
         ]
      },
      {  
         "title":"Shirt Front",
         "thumbnail":"http://ipadd/pub/media/catalog/product//b/l/blue-back_1_1.jpg",
         "elements":[  
            {  
               "type":"image",
               "source":"ipadd/pub/media/catalog/product//b/l/blue-back_1_1.jpg",
               "title":"Base",
               "parameters":{  
                  "left":325,
                  "top":329,
                  "colors":"#d59211",
                  "price":20,
                  "colorLinkGroup":"Base",
                  "fill":false
               }
            }
         ]
      },

   ]
]`

const validJsArray = eval(json)

Comments

0

JSON.stringify will clean that up for you.

var my_cool_object = [
        [
            {
                "title": "Shirt Front",
                "thumbnail": "http://ipadd/pub/media/catalog/product//b/l/blue-back_2.jpg",
                "elements": [
                    {
                        "type": "image",
                        "source": "http://ipadd/pub/media/catalog/product//b/l/blue-back_2.jpg",
                        "title": "Base",
                        "parameters": {
                            "left": 325,
                            "top": 329,
                            "colors": "#d59211",
                            "price": 20,
                            "colorLinkGroup": "Base",
                            "fill": false
                        }
                    }
                ]
            },
            {
                "title": "Shirt Front",
                "thumbnail": "http://ipadd/pub/media/catalog/product//b/l/blue-back_1_1.jpg",
                "elements": [
                    {
                        "type": "image",
                        "source": "ipadd/pub/media/catalog/product//b/l/blue-back_1_1.jpg",
                        "title": "Base",
                        "parameters": {
                            "left": 325,
                            "top": 329,
                            "colors": "#d59211",
                            "price": 20,
                            "colorLinkGroup": "Base",
                            "fill": false
                        }
                    }
                ]
            },
        ]
    ];
    var valid_json_string = JSON.stringify(my_cool_object);

Comments

0
var json = [ ...  ]
console.log(JSON.parse(JSON.stringify(json)));

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.