0

json string:

str = "{'Link': 'https://careers.powergrid.in/CCBaclogVacancy2018/c/default.aspx',
'Title': 'Apply Online'},{'Link': 'https://careers.powergrid.in/CCBaclogVacancy2018/c/login.aspx',
'Title': 'Login'},{'Link': 'media/pdf/details/all-india-govt-jobs/other-all-india-govt-jobs/8588011698.pdf',
'Title': 'Notification '},{'Link': 'http://www.powergridindia.com/', 'Title': 'Official Website'}"

expecting:

json = {'Link': 'https://careers.powergrid.in/CCBaclogVacancy2018/c/default.aspx',
'Title': 'Apply Online'},{'Link': 'https://careers.powergrid.in/CCBaclogVacancy2018/c/login.aspx',
'Title': 'Login'},{'Link': 'media/pdf/details/all-india-govt-jobs/other-all-india-govt-jobs/8588011698.pdf',
'Title': 'Notification '},{'Link': 'http://www.powergridindia.com/', 'Title': 'Official Website'}

i am trying with JSON.parse(str); it gives me error:

VM267:1 Uncaught SyntaxError: Unexpected token ' in JSON at position 1
at JSON.parse (<anonymous>)
at <anonymous>:1:6

How can i convert string to pure json.

please have a look into this.

6
  • 3
    Your Json is not correct. your trying to convet some kind of Array of Jsons but your not putting it inside Array. Commented Oct 28, 2018 at 17:28
  • 4
    Property names and string values must be quoted with " characters in JSON. Commented Oct 28, 2018 at 17:29
  • is there any way to do this using regular expressions?? Commented Oct 28, 2018 at 17:33
  • change all ' to " ? Commented Oct 28, 2018 at 17:33
  • no bro . it is coming from database Commented Oct 28, 2018 at 17:38

4 Answers 4

2

JSON.parse expects a well formed string. JSON data is written as name/value pairs. A name/value pair consists of a field name (in double quotes), followed by a colon, followed by a value (in double quotes if its a string value).

In addition, it looks like you're attempting to describe a collection of objects, so you should wrap it all inside an array... (note i'm using a `` ES2015 string, that allows multiline strings)

const str = `[{
    "Link": "https://careers.powergrid.in/CCBaclogVacancy2018/c/default.aspx",
    "Title": "Apply Online"
},
{
    "Link": "https://careers.powergrid.in/CCBaclogVacancy2018/c/login.aspx",
    "Title": "Login"
},
{
    "Link": "media/pdf/details/all-india-govt-jobs/other-all-india-govt-jobs/8588011698.pdf",
    "Title": "Notification "
},
{
    "Link": "http://www.powergridindia.com/",
    "Title": "Official Website"
}]`;
console.log(JSON.parse(str)[0].Title); // Apply Online
Sign up to request clarification or add additional context in comments.

Comments

1

Your json is invalid.

  • single quote should be double quote
  • objects should be wrapped by [] which indicate array

so to make it valid json, wrap objects with brackets and replace all single quote, and then parse it.

str = `[{'Link': 'https://careers.powergrid.in/CCBaclogVacancy2018/c/default.aspx',
'Title': 'Apply Online'},{'Link': 'https://careers.powergrid.in/CCBaclogVacancy2018/c/login.aspx',
'Title': 'Login'},{'Link': 'media/pdf/details/all-india-govt-jobs/other-all-india-govt-jobs/8588011698.pdf',
'Title': 'Notification '},{'Link': 'http://www.powergridindia.com/', 'Title': 'Official Website'}]`

json = JSON.parse(str.replace(/'/g, '"'))
console.log(json)

Comments

1
str = "what 'ever'";
str = str.replace(/'/gi,'"')

this is the way to replace all with regexp and replace()

4 Comments

Uncaught SyntaxError: Invalid or unexpected token
@KiranSyoutubechannel yes true my mistake. this is the right one.(editted)
it is changing ' to " but JSON.parse is showing error
This doesn't answer the question
0

You are missing the outermost wrapper for whatever array you have going on here. Also double quotes are the standard in json

Try wrapping your fields.

`{
   "items": [
      {
       "Link": "https://careers.powergrid.in/CCBaclogVacancy2018/c/default.aspx",
       "Title": "Apply Online"
      }, {
        "Link": "https://careers.powergrid.in/CCBaclogVacancy2018/c/login.aspx",
        "Title": "Login"
      }, {
        "Link": "media/pdf/details/all-india-govt-jobs/other-all-india-govt-jobs/8588011698.pdf",
        "Title": "Notification"
      }, {
        "Link": "http://www.powergridindia.com",
        "Title": "Official Website"
      }
    ]
}`

5 Comments

Typo! You forgot opening the first object on the items array
Yeah. I'm doing this from a phone, so i have to submit to preview
Traditional strings doesn't support multiline, you should use `` or the old-nasty + after each line
@sminutoli yes that is true, I'm just trying to make the format more obvious. You'd have to remove the whitespace in actual use
@sminutoli Fixed

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.