0

I have a string

{ city : Chicago , country : us , name : Somebody , plan : quarter90wtrial },{ city : New York , country : us , name : Somebody , plan : quarter90wtrial },{ city : Portland , country : us , name : Somebody , plan : quarter90wtrial },{ city :null, country :null, name : Somebody , plan : quarter90wtrial },{ city : Mexico City , country : mx , name : Aaron , plan : quarter90wtrial },{ city : Boston , country : us , name : Somebody , plan : quarter90wtrial },{ city : Los Angeles , country : us , name : Somebody , plan : quarter90wtrial },{ city : London , country : gb , name : Aaron , plan : quarter90wtrial },{ city : Los Angeles , country : us , name : Aaron , plan : quarter90wtrial },{ city : Chicago , country : us , name : Aaron , plan : quarter90wtrial }

What I am trying to do is turn this string into an object where I can iterate and print out the city, country, and name for each one. So far I have figured out how to split the string to print out each line individually.

str = str.split('},{');
for(var i =0; i < str.length; i++)
{
      alert(str[i]);
}

What I cannot figure out is how to convert the string to an object where I can print out the city, country, name, etc.

5
  • 4
    What you posted is not valid JSON, so you cannot use JSON.parse. What creates this string? Any chance you make it produce JSON? That would be the best solution. Commented Jul 19, 2017 at 18:30
  • From what I assume, your string is missing the enclosing [] without which it is not valid json Commented Jul 19, 2017 at 18:32
  • 1
    @ShubhamKhatri It's also missing a ton of double quotes around all the properties and values... Commented Jul 19, 2017 at 18:32
  • When producing JSON, you may want to use a validator such as jsonlint.com to validate whether the JSON that was created by your program is syntactically correct. Commented Jul 19, 2017 at 18:33
  • 1
    If you can generate correctly formatted JSON, this becomes entirely easy. As @FelixKling asked, what is generating the string above? Commented Jul 19, 2017 at 18:40

1 Answer 1

2

You had the correct initial idea.

However, after the split method, you will still need to map every pair of key:value like this:

var str = '{ city : Chicago , country : us , name : Somebody , plan : quarter90wtrial },{ city : New York , country : us , name : Somebody , plan : quarter90wtrial },{ city : Portland , country : us , name : Somebody , plan : quarter90wtrial },{ city :null, country :null, name : Somebody , plan : quarter90wtrial },{ city : Mexico City , country : mx , name : Aaron , plan : quarter90wtrial },{ city : Boston , country : us , name : Somebody , plan : quarter90wtrial },{ city : Los Angeles , country : us , name : Somebody , plan : quarter90wtrial },{ city : London , country : gb , name : Aaron , plan : quarter90wtrial },{ city : Los Angeles , country : us , name : Aaron , plan : quarter90wtrial },{ city : Chicago , country : us , name : Aaron , plan : quarter90wtrial }';

var result = str.split('},{').map(function(itemStr) {
    var itemObj = {};

    // here we map each "key:value" pair
    itemStr.replace(/.*?(\w+) *: *(.*?) *[,}]/g, function(match, key, val) {
        itemObj[key] = val;
    });
    return itemObj;
});

console.log(result)

You can see how the regular expression is working here: https://regex101.com/r/E5tg26/1

Hope it helps.

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.