0

Hey I would like to transform this string into JSON:

var newPart = '{\'' + name + '\': {\'content-type\' : ' +  type + ', \'content\': ' + content +  ',\'type\': \'content\'}}';

The result gives the following string:

{'partHtml': {'content-type' : text/html, 'content': dfg,'type': 'content'}}

I would like to get a JSON object from this string, i tried the following but it fails with the error : "SyntaxError: Unexpected token '"

newPart = JSON.parse(newPart);

I tried without the ' with a few changes but the problem persists.

Thank you for your help.

3
  • 2
    Change all single quotes to double. Commented Aug 25, 2015 at 10:53
  • 1
    all the strings must be quoted with double quotes { "partHtml": { "content-type": "text/html", "content": "dfg", "type": "content" } } Commented Aug 25, 2015 at 11:02
  • Your string does not represent JSON. See JSON specification at json.org Commented Aug 25, 2015 at 11:06

8 Answers 8

2

Although Lorenzo's answer is correct, is there a reason why you are building the string like this? Why not just build the object to start with?

var type = 'text/html';
var content = 'dfg';
var name ='partHtml'

var newPart = {};
newPart[name] = {content_type: type, content: content, type: 'content'};
Sign up to request clarification or add additional context in comments.

Comments

0

JSON strings are quoted using ", not '.
Therefore, your example should be:

var newPart = '{"' + name + '": {"content-type" : ' +  type + ', "content": ' + content +  ',"type": "content"}}';
newPart = JSON.parse(newPart);

Or, if you already have everything you need, you could construct the object immediately without using JSON.parse():

var newPart = {"name": {"content-type": type, "content": content, "type": "content"}};

Comments

0

This is a really bad practice and an unsafe method. You just need to create an object.

var newPart = {     
    "content-type" : "text/html",
    "content": "dfg",
    "type": "content"      
}

5 Comments

the object you're creating here is different than the one the OP needs. you left out the "partHtml" key.
Whatever this object looks like or contains, this is the most efficiency and safest method.
This is not an answer :)
Ok I missed the first key of the object. Please consider mikeecb's answer as the best one.
Yes, mikeecb's answer is correct, you'll probably want to remove this wrong one
0

This is not a valid JSON, so it's not possible to parse it. A valid version would look like this:

{"partHtml": {"content-type": "text/html", "content": "dfg", "type": "content"}}

You can't use the ' character, you need " instead.

Comments

0

JSON strings are quoted using "

`var newStr = '{"' + name + '": {"content-type" : "' + type + '", "content": "' + content + '","type": "content"}}';

var newJson = JSON.parse(newStr);

or

var newJson = eval('(' + newStr + ')');

Comments

0

Just replace this line with your

var newPart = '{\"' + name + '\": {\"content-type\" : \"' +  type + '\", \"content\": \"' + content +  '\",\"type\": \"content\"}}';

1 Comment

I did and same problem .. This is what i have now as a result but still the same problem: {'partHtml': {'content-type' : 'text/html', 'content' : 'fgh','type': 'content'}}
0

you have to be careful while merging strings. Instead of creating a string and face delimitation problems i suggest to create an object instance and assign properties on the fly.

var newPart = {};
newPart.name = {content : content};
newPart.name['content-type'] = type;

and so on..

Comments

-1

There's an error in your JSON. text/html is a string, but it's missing quotes.

Try to add them:

var newPart = '{"' + name + '": {"content-type" : "' +  type + '", "content": "' + content +  '", "type": "content"}}'

Please notice that if you want a lint valid JSON, you should use double quotes. Test your JSON in any online tool such as http://jsonlint.com/

{
    "partHtml": {
        "content-type" : "text/html",
        "content": "dfg",
        "type": "content"
    }
}

Moreover, if your variables contain double quotes, you'll get an error while parsing the JSON. This answer would work for your code, but I don't recommend it in real applications because it's a bad practice. For the best practice, please look at the addition that mikeecb did to my answer.

4 Comments

I fixed in order to have quote everywhere like this : {'partHtml': {'content-type' : 'text/html', 'content' : 'fgh','type': 'content'}} but same error : SyntaxError: Unexpected token '
please check again, I edited the answer and added fixes directly to your code.
What if any of your variables contains double quotes ?
In this case you'll get an error while parsing. This answer would work for the OP's example code, but I don't recommend it in real applications because it's a bad practice. For the best practice, look at the addition that mikeecb did to my answer. Thanks for pointing it out, I,'ll update my answer

Your Answer

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