Get values from JSON using JSON_VALUE in SQL Server - sql

I have the below JSON (Sorry I dont know how to format it!) and I am struggling to understand how to extract values at the different levels.
My code so far is this which brings back the data required from the first few columns..
,JSON_VALUE(jsonstring,'$[0].addPoint') Addpoint
,JSON_VALUE(jsonstring,'$[0].department') department
,JSON_VALUE(jsonstring,'$[0].subBuilding') subBuilding
,JSON_VALUE(jsonstring,'$[0].buildingNumber') Buildingnumber
,JSON_VALUE(jsonstring,'$[0].buildingGroup') buildingGroup
However I am not sure how I would get the below columns..
"mpan"
"serialnumber"
Can someone advise me as to what I am missing here? I haven't worked with JSON before and have googled but can't find a definite solution
[
{
"addPoint":null,
"department":null,
"subBuilding":null,
"buildingNumber":"1",
"buildingName":null,
"buildingGroup":null,
"poBox":null,
"subStreet":"The Arches",
"subLocality":null,
"stateRegion":"Lancashire",
"subAdministrativeArea":null,
"administrativeArea":null,
"superAdministrativeArea":null,
"countryCode":"GBR",
"countryName":null,
"dpsZipPlus":"1B5",
"formattedAddress":"TEST,,MANC,Lancashire,66666",
"welshSubStreet":null,
"welshStreet":null,
"welshSubLocality":null,
"welshLocality":null,
"welshTown":null,
"geographicInformation":null,
"additionalItems":{
"item":[
{
"key":"DATASOURCE",
"value":"tu_REGISTER"
}
],
"tmp":null
},
"groupedAdditionalItems":null,
"persons":null,
"uprn":null,
"lpi":null,
"blpu":null,
"streetDescriptor":null,
"streetInformation":null,
"companyInformation":null,
"dnbCompanyInformation":null,
"onsPointerInformation":null,
"classification":null,
"osAl2Toid":null,
"osItnToid":null,
"osTopoToid":null,
"voaCtRecord":null,
"voaNdrRecord":null,
"apOSAPR":null,
"rmUDPRN":"2744498",
"mrOccCountSpecified":false,
"alias":null,
"utilitiesInformation":{
"fuelType":1,
"fuelTypeSpecified":true,
"gasInformation":null,
"electricityInformation":{
"meterPoint":[
{
"mpan":"162558070",
"meter":[
{
"serialNumber":"D07W05001",
"type":"N"
}
],
"profileType":"02",
"timeSwitchCode":"811",
"lineLossFactorId":"531",
"standardSettlementConfiguration":"0151",
"energisationStatus":"E",
"energisationEffectiveFromDate":{
"day":5,
"daySpecified":true,
"month":12,
"monthSpecified":true,
"year":2014,
"yearSpecified":true
},
"distributorId":"16",
"gspid":"_G"
}
],
"tmp":null
},
"tmp":null
},
"organisation":null,
"street":"Clive Street",
"town":"MANCH",
"postCode":"r4d 1ES",
"locality":null
}]

You can use the following paths:
$[0].utilitiesInformation.electricityInformation.meterPoint[0].mpan
$[0].utilitiesInformation.electricityInformation.meterPoint[0].meter[0].serialNumber

Related

How to use Aggregate in AppSmith

In my database of MongoDB, I can't insert new register in the historial
I want insert the phone and start date
My model in Mongo is this
And the code what write is this
{
"aggregate": "dids",
"pipeline": [
{
"$match": {
"_id": ObjectId({{sPhone.selectedOptionValue}})
}
}
],
"cursor": {
"batchSize": 10
}
}
how insert $addFields in pipeline segment?
"$addFields": {
"history.contract_id": {{tableOfContracts.selectedRow._id}},
"history.start_date": {{dtpkStart.selectedDate}}
}
Yes is written below the match segment, throws error ('A pipeline stage specification object must contain exactly one field')
Any solution?
To insert data, you can consult it in the appsmith documentation in the following links.
Link to the explanation of mongodb methods: https://www.mongodb.com/docs/manual/reference/insert-methods/
Link to query syntax: https://docs.appsmith.com/reference/datasources/querying-mongodb/mongo-syntax
Query configuration:enter image description here

Can I refer to the Row/Document internal variables when filtering in Prisma?

How can I use row/document variables in filters and sorting?
As you know in SQL we can filter on joins beside the foreign key Something like this
Select * From A LEFT JOIN B on A.key = B.foriegnKey AND B.key IN A.currentSelection
or even in mongo lookup
collection('A').aggregate([{
$lookup: {
from: "B",
localField: "key",
foreignField: "foreignKey",
let: { A_currentSelection: "$currentSelection" },
pipeline: [{
$match: {
$expr: { $in: ["$key", "$$A_currentSelection"] }
}
}],
as: "matches"
}
},
])
But you can't do the following in Prisma
prisma.A.findMany({
include: {
B: {
where: {
'$A.currentSelection': {
has: "$B.key"
}
}
}
}
})
Regardless of the query itself, the idea is that I can access the current row/document variables in the query, I also know that I can modify the structure of the database to get around these kinds of issues but the database is already structured in a specific manner that might break some parts of the code and it's also not viable to change the structure just because Prisma is not lacking in this part.
At first, I was using a raw query to get around this and know I've created more complex relationships in the schema to fix this in Prisma in this case, but if anyone knows a more elegant solution then I'd be grateful

Use sprintf syntax inside logstash's sprintf syntax

For the below data structure:
{
"sprints": [
{
"id": 17193,
"name": "Sprint 12"
},
{
"id": 16510,
"name": "Sprint 11"
}
],
"velocityStatEntries": {
"16510": {
"estimated": {
"value": 49
},
"completed": {
"value": 36
}
},
"17193": {
"estimated": {
"value": 52
},
"completed": {
"value": 70
}
}
}
}
Given this, I want to be able to produce an Elasticsearch object that's easier to handle, by adding the values of the Estimated and Completed fields to the sprints with their matching IDs.
Ideally, I would like to handle this without writing Ruby, but I am not finding a logstash-native solution that handles this scnenario.
First, I split the data on the sprints field using split, so, I only have a single sprints object, and can use [sprints][id] to know what sprint I'm processing.
Then, I have attempted to work with the mutate filter, in one of two ways:
- using merge to add the [velocityStateEntries][] object to the
current sprint
- using add_field to add the two fields I need
Syntactically, is this possible? Ideally, I would want to be able to do a 'double substitution' of sorts, obtaining the estimated time for the current sprint something like:
add_field => {
"estimatedTime" => "%{[velocityStatEntries][%{[sprints][id]}][estimated][value]}"
}
but this only seems to work with a hardcoded format such as "estimatedTime" => "%{[velocityStatEntries][1234][estimated][value]}"
Do I have to use the Ruby format for this?
For what it's worth, the Ruby solution is very simple:
ruby {
code => "
sprintId = event.get('[sprints][id]');
estimated = event.get('[velocityStatEntries]['+(sprintId).to_s+'][estimated][value]');
completed = event.get('[velocityStatEntries]['+(sprintId).to_s+'][completed][value]');
event.set('[sprints][estimatedUnits]', estimated);
event.set('[sprints][completedUnits]', completed);
"
}

Pentaho: Getting only one in row from JSON input file

I am getting one JSON file from SFTP and trying it to insert into oracle but in the preview section i am getting only one row and only one row inserting into table. I already tried to modify to number to preview rows to 10000 but nothing working out.
{"postal_code":"XX","build_id":"XX","categories":[],"closed":false,"closed_reasons":[],"email":["XX"],"external_link":{"facebook":[],"yelp":[""]},"hq":false,"location":{"lat":xxxxx,"lon":xxxxx},"metro":"Cxxxxx, IL","naics_codes":[{"category_code":"XX","category_description":"XX"},{"category_code":"XX","category_description":"xxxxx "},{"category_code":"XX","category_description":"xxxxx "},{"category_code":xxxxx","category_description":"XX"}],"name":"XX","place_id":"XX","place_ids":["xxx","xx"],"sic_codes":[{"category_code":"XX","category_description":"XX"},{"category_code":"XX","category_description":"XX"}]}
Your example json is:
{
"postal_code":"XX",
"build_id":"XX",
"categories":[],
"closed":false,
"closed_reasons":[],
"email":["XX"],
"external_link":{
"facebook":[],
"yelp":[""]
},
"hq":false,
"location":{
"lat":xxxxx,
"lon":xxxxx
},
"metro":"Cxxxxx, IL",
"naics_codes":[
{
"category_code":"XX",
"category_description":"XX"
},
{
"category_code":"XX",
"category_description":"xxxxx "
},
{
"category_code":"XX",
"category_description":"xxxxx "
},
{
"category_code":xxxxx",
"category_description":"XX"
}
],
"name":"XX",
"place_id":"XX",
"place_ids":["xxx","xx"],
"sic_codes":[
{
"category_code":"XX",
"category_description":"XX"
},
{
"category_code":"XX",
"category_description":"XX"
}
]
}
So, if that's the total values in the response you're getting from the SFTP server, Pentaho is behaving well, because it's only one record. If you want PDI to recognize the fields inside that json, and split it's content, you need to specify the path to each field on the "Path" field available on the "Fields" tab on the "Json Input" step.
Try using the input json file as follows:
{
"data" : [
{"key":"val","key":"val"},
{"key":"val","key":"val"},
{"key":"val","key":"val"},
{"id":"666","name":"jnit"}
]
}

incorrect edge direction in infoviz's Force Directed Graph

I am trying to show force graph like "graphnode6.0-----> graphnode0.0 -------> graphnode15.0 " but when i contruct json object i always get "graphnode6.0------> graphnode0.0 <---------graphnode15.0"
My JSON object is
var json = [
{
"adjacencies": [
{
"nodeTo": "graphnode15.0",
"data": {"$type":"arrow",
"$direction":"['graphnode0.0','graphnode15.0']"}
}
],
"data": {
'$color': "#83548B",
'$type': "circle"
},
"id": "graphnode0.0",
"name": "graphnode0.0"
},
{
"adjacencies": [
{
"nodeTo": "graphnode0.0",
"data": {"$type":"arrow",
"$direction":"['graphnode6.0','graphnode0.0']"}
}],
"data": {
"$color": "#83548B",
"$type": "circle"
},
"id": "graphnode6.0",
"name": "graphnode6.0"
}
];
Am i making any mistake in this JSON structure ?
Thanks
Sumit
I had a similar problem but thanks to your quick demo, I think I've managed to fix both our issues :)
Use double quote inside your direction block.
"$direction":["graphnode1", "nodeZ"]
When I tried with single quotes
"$direction":['graphnode1', 'nodeZ']
the arrows would render in one direction only.
Hope it works for you!
You are using a string for the $direction property.
"$direction":"['graphnode6.0','graphnode0.0']"
It should be an array. Like this:
"$direction":["graphnode6.0","graphnode0.0"]
See this example code.