1

I would like to extract JSON entries from the main JSON bulk data, based on an input, in JavaScript. Each entry in the main JSON data has it's own unique ID, but the filter should be based on the text identifier rather than the ID. I would like to retrieve for example all entries that contain the word burg (Burg, BURG, bUrg, etc.) or any other given variety. This should of course also work with other search terms. I do not possess the JavaScript skills to do this.

In the data given below this should return 3 results. Obviously, the result should be the exact same JSON format.

Example JSON:

{"type":"FeatureCollection","features":[
    {"id":1,"text":"Cape Town"},
    {"id":2,"text":"Kimberley"},
    {"id":3,"text":"Beaufort West"},
    {"id":4,"text":"Johannesburg Park"},
    {"id":5,"text":"Germiston"},
    {"id":6,"text":"Pietermaritzburg"},
    {"id":7,"text":"Durban"},
    {"id":8,"text":"Bellville"},
    {"id":9,"text":"Wellington"},
    {"id":10,"text":"Huguenot"},
    {"id":11,"text":"Worcester"},
    {"id":12,"text":"Matjiesfontein"},
    {"id":13,"text":"Laingsburg"},
    {"id":14,"text":"Prince Albert"},
    {"id":15,"text":"Hutchinson"},
    {"id":16,"text":"De Aar"},
    {"id":17,"text":"Warrenton"}
]}
5
  • 1
    Parse it properly then select what you need out of the JS object. Commented Nov 7, 2017 at 19:52
  • Yeah I'm afraid that won't help me a lot.... Current code already parses the whole JSON, but I don't know whether it is easier to filter before parsing or after parsing.. Commented Nov 7, 2017 at 19:56
  • "but I don't know whether it is easier to filter before parsing or after parsing" --- so, implement anything and make a decision based on facts? Who do you trust more - yourself or strangers in the internet? Commented Nov 7, 2017 at 19:58
  • So, what should the sample JSON output? Commented Nov 7, 2017 at 20:21
  • The sample JSON output should contain the exact same format as given in the example JSON, but then with only the identifiers (+ contents) in which the text contains a search/word entry. In the example this would result in returning only 3 entries. Commented Nov 7, 2017 at 20:27

1 Answer 1

4

Do not use JavaScript for this. Use SQL and its LIKE operator instead.

But if you insist on using JavaScript for this…

Just like HTML, regular expressions cannot fully parse JSON because of serialization.

Filtering after JSON.parse is quite easy however; you can use the Array.prototype.filter() method:

var s = '{"type":"FeatureCollection","features":[{"id":1,"text":"Cape Town"},{"id":2,"text":"Kimberley"},{"id":3,"text":"Beaufort West"},{"id":4,"text":"Johannesburg Park"},{"id":5,"text":"Germiston"},{"id":6,"text":"Pietermaritzburg"},{"id":7,"text":"Durban"},{"id":8,"text":"Bellville"},{"id":9,"text":"Wellington"},{"id":10,"text":"Huguenot"},{"id":11,"text":"Worcester"},{"id":12,"text":"Matjiesfontein"},{"id":13,"text":"Laingsburg"},{"id":14,"text":"Prince Albert"},{"id":15,"text":"Hutchinson"},{"id":16,"text":"De Aar"},{"id":17,"text":"Warrenton"}]}';
var input = "burg";
var o = JSON.parse(s);
o.features = o.features.filter(e => RegExp(input, 'i').test(e.text));
console.log(JSON.stringify(o));

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

3 Comments

Thanks!! That seems to do the trick. One short additional Q: how could I replace the now fixed string 'burg' with a text-variable? I've tried finding the answer in the documentation of Javascript RegEx, however I obviously was not succesfull.
@Coryza if you like an answer, the proper thanks is to upvote and/or accept. (I'll change the snippet in a bit to answer the follow-up.)
You're absolutely right.. due to my exciteness I forgot about it ;). Thanks again.

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.