JavaScript code - UNIX grep?

Hi

I am new to JavaScript & haven't done much work with it, but have mainly experience with UNIX. I have a piece of code where I want to grep (excuse the UNIX language :D) for a id and get the number from that.

		
{
	"time": 900,
	"avail": 1,
	"price": 0,
	"datetime": "2019-11-09T15:00:00+00:00"	
}

{
	"name": "janice-bishop",
	"date": "2019-11-09",
	"event_ID": 155643,
	"person_ID": 18709,
	"resource_ID": 561,
	"_links" : 
}

Essentially what I want is:

if avail = 1 

then grep "event_ID"

& then want to take the number which would be the event id? Not sure how possible this is within JavaScript but something along those lines like you would do with a UNIX shell script.

You do not need to grep, search or parse to get a value from an object in Javascript.

You simply take any Javascript object and reference the element you want, for example:

var obj1 = {
"time": 900,
"avail": 1,
"price": 0,
"datetime": "2019-11-09T15:00:00+00:00"
}
var obj2 = {
"name": "janice-bishop",
"date": "2019-11-09",
"event_ID": 155643,
"person_ID": 18709,
"resource_ID": 561,
"_links" :"blank"
}

if ( obj1.avail == 1)
{
 var event = obj2.event_ID;
}

There are other ways to get the same object property, for example:

var event = obj2.event_ID;
var eventful = obj2['event_ID'];

Hi Neo!

Thank you for your response!. I understand what you are saying & makes sense. Thanks very much. My only issue is that the value "event ID" is dynamic & runs through an API so I don't run nor set the values in an object if that makes sense?

I see no problem in testing dynamic values coming from whatever source. As long as you can grab the data all is fine.

And since you have example data, the data is obviously there... . If it helps you describe further more in detail what you are doing. Maybe one can give additional hints.

Since you have a grip of what you want to do it's the only task to work out the code for your thoughts. :wink:

According to your post:

"event_ID": 155643,
"event_ID"

is a JSON property sent by the API as a key : value pair. Of course the value of this property will change, but the key is the same.

Remember, JSON is an object with key : value pairs.

Before you start parsing JSON objects, you should probably take a step-back and try to understand what a JSON object is and how they are used in APIs. Because, if you understood the bare minimum about JSON and Javascript you would not ask "how do I grep a Javascript JSON object to get a value based on a key"... which is essentially what you asked in your original post.

There is a big reason why JSON APIs are the most popular APIs on the web today; and there is a reason that most web developers process JSON objects with Javascript. It is trivial to access values in the object (as long as the JSON object is formatted correctly, LOL) and you don't need to write parsers, use 'grep', or 'awk' or 'sed' or anything like that when you are using Javascript.

You just receive the JSON object via your API and deference the key as I illustrated to get the value associated with the key. Of course the value changes, but the key does not change. If the key changed, 'grep' would not help you either, because you must know what the keys are in your API, that is the very, most "API 101" basic, fundamental thing you need to know with working with API and JSON with key:value pairs.

WIkipedia ... background info for you:

.

Note that the wikipedia blurb above calls the key-value pairs, attribute-values ; but it is the same thing, just a different name.

And forget the "XML" part of the blurb above. JSON is much simpler and easier to use for APIs. When I just coded the new "computer science trivia" feature here, all the APIs return JSON objects. These days, every API I write all return JSON; and when I build some new mobile forum pages later this year, all will be based on JSON API calls.

JSON is your friend.

if (response.getStatusCode() != 200) {
api.fail("HTTP error: " + response.getStatusCode());
} else {
var responseBody = response.getResponseBody();
var jsonData = JSON.parse(responseBody);
var eventId = api.getValue("event_id ", jsonData.event_id);
api.setValue("eventID_Stamp", eventId);
}

This is what I have created

Congrats on starting to learn JS and JSON APIs.

Does your code work?

1 Like

Yes all works good & I understand! Thanks for your help guys!

1 Like

Great.

OBTW, I don't have the exact JSON API output in front of me and your JS code which calls the API, so this might be way off base; but this code seems overly complex to me; but then again, I don't have the JS code which calls the API or example JSON data from the API.

If you want me to take a closer look; please post your JS code which calls the API and some sample JSON output from the API.

Either way, glad you have it working now.

Cheers.