1

i have used Object.parses() but getting error

var obj = '
   "users": [
 { "name":"John", "age":30, "city":"New York"},
 { "name":"Mike", "age":25, "city":"new jersey"},   
]'
7
  • 4
    What is the error that you are getting? and what is Object.parses()? Commented Jul 10, 2019 at 13:05
  • 6
    Did you mean JSON.parse()? Commented Jul 10, 2019 at 13:06
  • 3
    I'd suggest that you use JSON.parse() except that the string you've posted isn't valid JSON Commented Jul 10, 2019 at 13:06
  • Yes JSON.parse is right if you want to parse a object Commented Jul 10, 2019 at 13:08
  • 2
    @Owaisp that still isn't valid JSON. and it isn't valid JavaScript either. You need to pay closer attention to what you're typing. Using an IDE which can highlight syntax errors, and also a JSON checker like jsonlint.com would probably help you sort your code out before you run it. See my answer below for a summary anyway. Commented Jul 10, 2019 at 13:13

2 Answers 2

5

Although you haven't mentioned JSON explicitly, this data looks like JSON. You can use JSON.parse() to turn JSON strings into JavaScript variables

However, the string you've posted isn't actually valid JSON because of a couple of syntax errors. You can fix those to get (what I assume is) the intended object structure:

1) remove the extra double-quote before new jersey

2) add curly braces at either end to make it into a valid object.

3) remove extra comma after the last array entry (although a lot of parsers will tolerate this in fact)

So you end up with

{ 
  "users": [
    { "name":"John", "age":30, "city":"New York"},
    { "name":"Mike", "age":25, "city":"new jersey"}
  ]
}

And this can be parsed easily:

var obj = '{ "users": [{    "name": "John",    "age": 30,    "city": "New York"  },  {    "name": "Mike",    "age": 25,    "city": "new jersey"  }]}';

var data = JSON.parse(obj);

console.log(data);
console.log("----------");

//example of gettng a specific property, now it's a JS variable
console.log(data.users[0].name);

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

8 Comments

I have used the same but when i assigning the following string object var string = { "users": [ { "name": "Andrew", "pets": [ { "name": "Layla", age: 12 }, { "name": "Poopers", age: 2 }, { "name": "Noelle", age: 1 }, { "name": "Huckleberry", age: 1 } ] }, { "name": "Sergio", "pets": [ { "name": 'Athena', age: 4 }, { "name": 'Anubis', age: 8 }, { "name": 'Fluffy', age: 1 } ] }, ] } JSON.parse() is not working for this
That's because it isn't JSON, it's already an object, you don't need to parse it into a new object. JSON.parse() expects to be given a string value, not an object. Remember: JSON is a way of writing objects within a string (so you can send it in a HTTP request, for example). If you're just declaring a variable then that's nothing to do with JSON, and you don't need JSON for that. To make that data be JSON within a JavaScript context you would have to enclose it in quote marks so that JavaScript sees it as a string. And then you could parse that, just like the example in my answer.
...But it's usually only useful to parse JSON if the data has come from another source rather than being hand-written into the code. My answer is just a theoretical example (based on what you were trying to do in your original code in the question).
this data i m getting as string its not a object.
The way you've written it in your comment, i.e. var string = { "users": [.... show that it's not a string - you've started with a {. That makes it an object literal. If you were creating a string it would start with var string = '{ "users": [... i.e. adding a ' quote mark at the start. But also that would give you other problems - you've got single quotes within the data instead of doubles (e.g. "name": 'Athena'). If it was a string, that would make the JSON invalid. JSON (unlike JavaScript) requires all property names and all string values to be always double-quoted.
|
1

First, correct your string. It should look like on inserted snippet. Second, use JSON.parse()

var t = '{"users": [{ "name":"John", "age":30, "city":"New York"},{ "name":"Mike", "age":25, "city":"new jersey"}]}'; 
    
     
     var obj = JSON.parse(t);
     console.log(obj["users"][0].name);
     console.log(obj["users"][0].age);
     console.log(obj["users"][0].city);

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.