How better to implement searching values in json?

There is a json view

[
{
 "name": "Adhi Kot",
 "id": "379",
 "nametype": "Valid",
 "recclass": "EH4",
 "mass": "4239",
 "fall": "Fell",
 "year": "1919-01-01T00:00:00.000",
 "reclat": "32.100000",
 "reclong": "71.800000",
 "geolocation": {
 "type": "Point",
 "coordinates": [
71.8,
32.1
]
}
}
]


As better without busting the display of values (any) for recclass ?
April 3rd 20 at 17:19
4 answers
April 3rd 20 at 17:21
Using indexOf you can. This is the best that comes to mind, i.e. the direct search "in a forehead" without unnecessary computation. Better only search without a search, when known in advance the index from which to take values.

var json_str = `,{"name":"Adhi Kot","id":"379","nametype":"Valid","recclass":"EH4","mass":"4239","fall":"Fell","year":"1919-01-01T00:00:00.000","reclat":"32.100000","reclong":"71.800000","geolocation":{"type":"Point","coordinates":[71.8,32.1]}},{"name":"Adzhi-Bogdo (stone)","id":"390","nametype":"Valid","recclass":"LL3-6","mass":"910","fall":"Fell","year":"1949-01-01T00:00:00.000","reclat":"44.833330","reclong":"95.166670","geolocation":{"type":"Point","coordinates":[95.16667,44.83333]}}`;
var i = 0;
while((i=json_str.indexOf('"recclass":"',i))!==-1) {
i+=12;
 let j = json_str.indexOf('"',i);
 let val = json_str.substring(i,j);
 console.log(val); //print next value
}
Will break if some value will have the string "reclass" - lilian.Stroman72 commented on April 3rd 20 at 17:24
@Cedrick_Johnston9, of course!

Optimization and versatility were always opposed to each other and will resist further.

The only question is, what is the chance that inside value will recclass in quotation marks without slashes followed by a colon and another quote (also without the slash)? As for me, almost zero. - andres.Lockman commented on April 3rd 20 at 17:27
@destini_Halvorson, if the code is known to detractors, the probability of this will increase in multiples and will be a hole for attack.
STO Security Through Obscurity.

PS as the variant of events: field
"description":"here we treat 'recclass':'bla-bla-bla' :)"
- lilian.Stroman72 commented on April 3rd 20 at 17:30
@Cedrick_Johnston9, detractors can take advantage of any hole :) not necessarily in the code. - andres.Lockman commented on April 3rd 20 at 17:33
April 3rd 20 at 17:23
this is an array of objects?

data = [
 {"name":"Adhi Kot","id":"379","nametype":"Valid","recclass":"EH4","mass":"4239","fall":"Fell","year":"1919-01-01T00:00:00.000","reclat":"32.100000","reclong":"71.800000","geolocation":{"type":"Point","coordinates":[71.8,32.1]}},
 {"name":"Adzhi-Bogdo (stone)","id":"390","nametype":"Valid","recclass":"LL3-6","mass":"910","fall":"Fell","year":"1949-01-01T00:00:00.000","reclat":"44.833330","reclong":"95.166670","geolocation":{"type":"Point","coordinates":[95.16667,44.83333]}}
]

const find = (rec, key) => (data.find(v => v.recclass === rec) || {})[key]

find('EH4', 'mass')
// 4239
Personally, I interpret the words "got the json" as is "is a string in json format". Well, from context it is rather a line than indented. - andres.Lockman commented on April 3rd 20 at 17:26
@destini_Halvorson, I personally interpret the words "got the json" as is "JSON.stringify()" and do what you want :) - talia_Jones50 commented on April 3rd 20 at 17:29
@lindsey23, Yes, but if you convert from JSON here and there, then it is it is not about the optimization, and the issue that is what the question is. - andres.Lockman commented on April 3rd 20 at 17:32
April 3rd 20 at 17:25
algorithm Boyer-Moore, just for the sample that does not change
April 3rd 20 at 17:27
If no deserialization, then you can parse the key and value.
1. When we find "recclass" - check value for a match. If match - found.
2. You can make an array that will contain only the values "recclass" in accordance with the items in json.

Find more questions by tags JavaScriptJSONAlgorithms