0

Is there a way to consistently exctract city names from strings that look like this:

{"id":25,"title":"Buenos Aires"}
{"id":26,"title":"Chicago"}

I was thinking about starting from 3 rd character from the end and then stopping at second quotation mark but I didnt find the way to do it.

4
  • Is this extracting from a json file or a JavaScript file? Commented Dec 16, 2018 at 20:47
  • Simple JSON parsing : w3schools.com/jsref/jsref_parse_json.asp Commented Dec 16, 2018 at 20:48
  • thanks, that was easy Commented Dec 16, 2018 at 20:48
  • @user1743703 look at Eriks answer below then. Commented Dec 16, 2018 at 20:49

1 Answer 1

2

The data is JSON formatted, you can decode it to js objects directly

var a = JSON.parse('{"id":25,"title":"Buenos Aires"}'), 
    b = JSON.parse('{"id":26,"title":"Chicago"}');

console.log(a.title); // prints Buenos Aires
console.log(b.title); // prints Chicago
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.