jsonpath select the first element in the array or element - jsonpath

I'm creating an extension for osTicket. I need to know the history of ticket statuses over time. For this reason, I read the status from the ost_thread_event.data column from the database. The date column is not always JSON with status key. Sometimes it is NULL or JSON without status key.
SELECT id, data, timestamp FROM ost_thread_event;
id
data
timestamp
1
NULL
2021-09-15 09:28:53
2
{"status":6}
2022-03-16 11:52:06
3
{"status":[2,"Completed"]}
2022-03-17 10:27:50
4
{"team":1}
2022-12-07 13:40:27
I want to create a JSONPath that will return me only a status digit.
For {"status":6} it is $.status
For {"status":[2,"Completed"]} it is $.status[0]
Unfortunately, I don't know how to combine these two queries. If the element is an array take the first element from the array, otherwise take the element. How can I combine it?

Related

To query Last 7 days data in DynamoDB

I have my dynamo db table as follows:
HashKey(Date) ,RangeKey(timestamp)
DB stores the data of each day(hash key) and time stamp(range key).
Now I want to query data of last 7 days.
Can i do this in one query? or do i need to call dbb 7 times for each day? order of the data does not matter So, can some one suggest an efficient query to do that.
I think you have a few options here.
BatchGetItem - The BatchGetItem operation returns the attributes of one or more items from one or more tables. You identify requested items by primary key. You could specify all 7 primary keys and fire off a single request.
7 calls to DynamoDB. Not ideal, but it'd get the job done.
Introduce a global secondary index that projects your data into the shape your application needs. For example, you could introduce an attribute that represents an entire week by using a truncated timestamp:
2021-02-08 (represents the week of 02/08/21T00:00:00 - 02/14/21T12:59:59)
2021-02-16 (represents the week of 02/15/21T00:00:00 - 02/22/21T12:59:59)
I call this a "truncated timestamp" because I am effectively ignoring the HH:MM:SS portion of the timestamp. When you create a new item in DDB, you could introduce a truncated timestamp that represents the week it was inserted. Therefore, all items inserted in the same week will show up in the same item collection in your GSI.
Depending on the volume of data you're dealing with, you might also consider separate tables to segregate ranges of data. AWS has an article describing this pattern.

How to show records according to time and whose status is less than 2 in GQL

I am new in GQL and facing problem while fetching records from cloud data store. I want to show records according to time when it is saving in data store and whose status is less than 2(i.e 0 or 1). Users whose details are saved recently comes on top in listing and then others. While saving details I am storing their timestamp also. Here is my query to retrieve the details whose status is 0 (which is working fine) but I want to retrieve details whose status is both 0 or 1.
"NOTE: Status datatype is string in datastore."
According to the GQL rule we can't use OR operator in it. So, what will be the solution for it. Anyone knows?
SELECT * FROM Users WHERE __key__ HAS ANCESTOR KEY(UsersDetails, 9623495224) AND status = '0' ORDER BY sent_on desc
Instead of using OR you can do the following workaround:
SELECT * FROM Users WHERE __key__ HAS ANCESTOR KEY(UsersDetails, 9623495224) AND status >= 0 AND status <= 1 ORDER BY sent_on desc
Note that for this case you should convert the data to int. You can check the reference here

RobotFramework: Getting string value back from sql query

Doing a database query that returns multiple rows...
${visits_for_this_patient}= Select from Database
... Select to_char(visit_date, 'YYYY-MM-DD') from patient_visits where patient_id=${patient_id} order by visit_date
${list_of_visits_for_this_patient}= create list ${visits_for_this_patient}
Row Count Should Be Equal To X
... Select * from patient_visits where patient_id=${patient_id}
... ${expected_number_of_patients_for_this_visit}
... ${SPONSOR_NAME}
How can i take a specific row's string value back without the padding? Im getting like
(b'2017-03-03',)
when i try ${visits_for_this_patient[0]}
You have to go one level deeper - the return value of а db query is a list of tuples (that's how the underlying python modules return the data).
E.g. it looks like this:
[(b'2017-03-03',)]
When you call ${visits_for_this_patient[0]}, you get the 1st member of the list - a tuple (what you saw yourself). To get the actual value, just get that value's first member (the 1st member of the tuple); so simply:
${visits_for_this_patient[0][0]}
The end value of that should be 2017-03-03 (as a string).

DynamoDB Last Evaluated Key Expiration?

My application ingests data from a 3rd party REST API which is backed by DynamoDB. The results are paginated and thus I page forward by passing the last evaluated key to each subsequent request.
My question is does the last evaluated key have a shelf life? Does it ever expire?
Let's say I query the REST API and then decide to stop. If I save the last evaluated key, can pick up exactly where I left off 30 days later? Would that last evaluated key still work and return the correct next page based on where I left off previously?
You shouldn't think of the last evaluated key like a "placeholder" or a "bookmark" in a result set from which to resume paused iteration.
You should think of it more like a "start from" place marker. An example might help. Let's say you have a table with a hash key userId and a range key timestamp. The range key timestamp will provide an ordering for your result set. Say your table looked like this:
user ID | Timestamp
1 | 123
1 | 124
1 | 125
1 | 126
In this order, when you query the table for all of the records for userId 1, you'll get the records back in the order they're listed above, or ascending order by timestamp. If you wanted them back in descending order, you'd use Dyanmo DB's scanIndexForward flag to indicate to order them "newest to oldest" or in descending order by timestamp.
Now, suppose there were a lot more than 4 items in the table and it would take multiple queries to return all of the records with a userId of one. Well, you wouldn't want to have to keep getting pages and pages back, so you can tell Dynamo DB where to start by giving it the last evaluated key. Say the last result for the previous query was the record with userId = 1 and timestamp = 124. You tell Dynamo in your query that that was the last record you got, and it will start your next result set with the record that has userId = 1 and timestamp = 125.
So the last evaluated key isn't something that "expires," it's a way for you to communicate to Dynamo which records you want it to return based on records that you've already processed, displayed to the user, etc.

How to retrieve data that contains null value for some field in sqlite?

I am fetching rows of record from my sqlite table for blackberry application.But certain fields contain a null and i get a run time error of No stack trace at those field.How i can i continue fetching data irrespective of their null fields.
This is how my table looks
Date Bill Receipt
12/5/2012 50
16/4/2012 40 20
1/6/2012 50 30
It gives me error for that third element of first row.
This is my code used for retrieving values
grid.insert(new LabelField(r.getString(k))
continuously going through 3 for loops-one for row,one for column and one for each column of database table.
If anyone aware on a solution to this,please share.Thanks.
Use Row.getObject() instead of Row.getString(). It will return 'null' for null values without throwing an exception. If non-null, you can then do a type check, or just cast to String if you know it will always be of type string.

Resources