BigQuery - Extract all array elements inside the array Dynamically - sql

I have already posted this question, but now Im trying to achieve this with BigQuery.
I have JSON data like below.
{
"userid": null,
"appnumber": "9",
"trailid": "1547383536",
"visit": [{
"visitNumber": "1",
"time": "0",
"hour": "18",
"minute": "15"
},
{
"visitNumber": "2",
"time": "2942",
"hour": "18",
"minute": "15"
}
]
}
I want to extract the visit array values dynamically.
Like below: (pipe demited column)
userid,appnumber| trailid |
visit.visitnumber | visit.time | visit.hour | visit.minute |
visit.visitnumber | visit.time | visit.hour | visit.minute
If you see I have 2 json elements inside the visit array. So I want to extract visitNumber, time, hour, minute dynamically. Sometime I may have 3 or 5 values inside the array, so It should extract all 3 or 5 json automatically(I mean dynamically).
There is a common way to extract this like JsonExtractScalar(JsonExtract(visit,'$.[0].visitnumber') (This syntax may be wrong, but similar syntax we'll use. So here Im manually using [0] to extract fist element in the array.
If it has 10+ elements then I should use [0]...[1]...[2]....[10]. This thing I want to solve, somehow without mentioning all elements it should dynamically pick all 10 elements and extract it.
Could someone help me with the extract queries?

Related

Comparing numbers in JSON Schema

I have number property in JSON schema
"years": {"type": "number", "pattern": "^([0-9]|10)$"}
I want to match this number in a condition where I need to check whether number is less than 3, is there a way to do it ? I tried
"if": {"properties": {"years": {"anyOf": [0,1,2]}}
you want exclusiveMaximum. see https://json-schema.org/understanding-json-schema/reference/numeric.html#range
note that you may want minimum: 0 to exclude negative numbers.
you may also want type: integer instead of type: number if you don't want to include fractional numbers.
pattern is incorrect as it applies to strings, not numbers.
anyOf takes a schema, not values, but you could use enum: [0, 1, 2] if those are the only allowed values.

How to count the number of event based on JSON field structure in Splunk

I want to count the number of occurrence of a specific JSON structure. For example in my event there is a field called data which its value is JSON . but this field can have a variety of structures. like:
data = {a: "b"}
data= {d: "x", h: "e"}
...
now I want to know how many event has data with each JSON structure and I don't care about values only keys are matter.
Try one of these 2:
index=ndx sourcetype=srctp data=*
| stats dc(data) as unique_data
Or
index=ndx sourcetype=srctp data=*
| stats values(data) as data_vals

How to get a formatted date and time string from `now`?

I'm using "Red Programming Language" version "0.6.4" on Windows and making a command line application.
I don't know much Red language and I don't understand many things. I did go over "work in progress" docs at (https://doc.red-lang.org/en/) before asking here.
I need to get a date and time string formatted as yyyymmdd_hhmm.
I've started with code like this:
Red []
dt: to string! now/year
print dt
which gives me 2019 but I need the other things month, day and time to obtain something like 20190608_2146
I tried also:
Red []
dt: to string! now/precise
print dt
which gives me 8-Jun-2019/21:47:51.299-07:00 but again what I needed was 20190608_2147
Question:
How to modify the code above to obtain something like 20190608_2147 from now?
Thank you.
I have written a script for Rebol and Red called 'Form Date' that will format dates/times in a similar fashion to STRFTIME. The Red version is here.
do %form-date.red
probe form-date now "%Y%m%d_%H%M"
print first spec-of :form-date
Within the script are individual snippets of code used for formatting the various components of a date! value.
You don't need the script for your specific example though, you can extract and join the various components thus:
date: now
rejoin [ ; reduce-join
form date/year
pad/left/with date/month 2 #"0"
pad/left/with date/day 2 #"0"
"_"
pad/left/with date/hour 2 #"0"
pad/left/with date/minute 2 #"0"
]
As the above solution has some problems under Rebol2 here a variation that works with Rebol and Red the same way
date: now
rejoin [
date/year
next form 100 + date/month
next form 100 + date/day
"_"
next form 100 + date/time/hour
next form 100 + date/time/minute
]
Here is another way:
rejoin [
now/year
either 10 > x: (now/month) [join "0" x][x]
either 10 > x: (now/day) [join "0" x][x]
"_"
either 10 > x: first (now/time) [join "0" x][x]
either 10 > x: second (now/time) [join "0" x][x]
]
Red has pad, so rgchris' answer up there is good. Yet, there is no need for date: now as rgchris has done:
rejoin [
now/year
pad/left/with now/month 2 #"0"
pad/left/with now/day 2 #"0"
"_"
pad/left/with first (now/time) 2 #"0"
pad/left/with second (now/time) 2 #"0"
]

vue draggable table with fixed column

I have a table of 2 columns, one column has a static list of values and the next column I need to be draggable so that I can move the cells in the second column up and down etc without affecting the first column.
Use case is a list of time slots and second column is a persons name, I will add name to the cell but then might want to drag that to a different time slot.
I have got table showing the times but not able to get the second bit working- the most important bit.
Any idea how to do this, maybe a table isn't the way to go?
Thanks
Here is an updated version where you can drag into the timeslots. I put both views into one to make it a bit easier to read.
I changed the timeslot structure to be:
times: [
{ id: 1, time: "09:00", people: [] },
{ id: 2, time: "09:30", people: [] },
{ id: 3, time: "10:00", people: [] }
],
So now you are just dragging the people into the times.people arrays.

how to read attribute from json

I want to read values of a json (message) object which has array in it.
This below query helps for immediate properties in d.
traces | extend d = parsejson(message) | d.Timestamp, d.Name;
How do I read property part of an array within d (message). For example if I want to read all street values in below message .. how to do ? This is kind of needing a loop
message
{
"Timestamp": "12-12-2008",
Name: "Alex",
address: {
[{"street": "",zip:""},{"street":"", "zip":""}]
}
}
One way to do this would be using the mvexpand operator (see documentation).
It will output a single row for each element in your array which you could iterate over.
So in your example, running:
traces | extend d = parsejson(message) | mvexpand d.address
Will output a row for each address.