1

The json response return (from ajax function) :

{ "shoe_products":"{name: 'nike pegasus 35',data: [56.00,43.00,32.00], stack: 'Nike'},{name: 'adidas ultraboost',data: [32.00,34.00,35.00,0,55.0], stack: 'Adidas'}" }

This is string after I debug it using alert(typeof data.myjson). The problem is, I can't convert it to object.

var myobj = JSON.parse(data.shoe_products); //this will return Unexpected token n in JSON at position 1

But if I do the code below, it return as an object.

var test = [{"shoe_products":"{name: 'nike pegasus 35',data: [56.00,43.00,32.00], stack: 'Nike'},{name: 'adidas ultraboost',data: [32.00,34.00,35.00,0,55.0], stack: 'Adidas'}"}];

Ajax function

 $.ajax({
            type: "POST",
            url: urlLinkHere,
            data: { "year" : year },
            success: function(data) {

                var myobj = JSON.parse(data.shoe_products);

            }
        });

    });
5
  • In Json, property names need to be surrounded by quotes. e.g. {"name":...} instead of {name:...} Commented Jan 10, 2019 at 3:55
  • in my case, how do I convert all these property names with double quotes? Commented Jan 10, 2019 at 3:56
  • If you can control the format of the source data, I'd try to make sure it produces valid Json. If not, you'll need a non-standard parser. See this question. Commented Jan 10, 2019 at 4:00
  • I could control the format of the source data, but it will be ugly though because I will use str_replace for each of the property name (name, data, stack) Commented Jan 10, 2019 at 4:03
  • seems like i am still unable to convert to object after changing all the property name with double quotes Commented Jan 10, 2019 at 4:08

1 Answer 1

1

Perhaps you could consider the following:

  1. Surround any "key" in the json string with double quotes via this: .replace(/([a-z]+)\w*c/gi, "\"$1\":"). The idea here is to match any string that's followed by : (which we consider to be a key) and wrap those matches with the double quotes.
  2. Next, surround the string from step 1 with [ and ], seeing that the data in shoe_products is actually an array (there are multiple objects in the shoe_products string that are separated by a ,)

So, something along these lines:

// Your input data
var test = [{"shoe_products":"{name: 'nike pegasus 35',data: [56.00,43.00,32.00], stack: 'Nike'},{name: 'adidas ultraboost',data: [32.00,34.00,35.00,0,55.0], stack: 'Adidas'}"}];

var shoe_products = test[0].shoe_products;

// The shoe_products data is organised as a list of data, so surround with [] brackets
// to achieve valid JSON array
var validJsonString = '[' + shoe_products
// Surround all json keys with double quotes. These are matched by any string followed by
// a colon
.replace(/([a-z]+)\w*:/gi, "\"$1\":") 
// Replace any other single quote with double quotes
.replace(/'/gi,'"') + ']'


var jsonObject = JSON.parse(validJsonString);

console.log(jsonObject)

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

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.