Create POJO Class for JSON - json

Below is the json object, I need to create the pojos. I had tried couple of things but not able to find anything relevant. Here the key itself is varying/dynamic
{
"ABC" : {
"values": {
"orr": {
"data": {
"1574065955000": 0.2,
"1574066955000": 0.34,
"1574067955000": 0.6899
}
},
"cpu": {
"data": {
"1574065955000": None,
"1574067955000": None
}
}
}
},
"DEF" : {
"values": {
"ups": {
"data": {
"1574065955000": 0.01,
"1574066955000": 0.09
}
},
"cpu": {
"data": {
"1574065955000": 0.09,
"1574066955000": 0.04,
"1574067955000": 0.0,
"1574068955000": 0.001
}
}
}
},
"scores_reference": {
"low_deviations": "0.0 - 0.4",
"moderate_deviations": "0.4 - 0.7",
"high_deviations": "0.7 - 1.0"
}
}
Can someone please suggest how can I create the pojos ? I tried multiple things but is not working out for me.

You can use any online JSON to POJO converter links and paste you sample JSON to get the corresponding POJO. following are some of the links :
http://pojo.sodhanalibrary.com/
http://www.jsonschema2pojo.org/
https://www.freecodeformat.com/json2pojo.php

Related

Find the average value in MongoDB from JSON

In my MongoDB (export from JSON file) I have database "dab" with structure like this:
id:"1"
datetime:"2020-05-08 5:09:56"
name:"namea"
lat:55.826738
lon:45.0423412
analysis:"[{"0":0.36965591924860347},{"5":0.10391287134268598},{"10":0.086884394..."
I'm using that db for spark analysis via MongoDB-Spark Connector.
My problem is field "analysis" - I need average result for all values from every interval ("0", "5", "10", ..., "1000"), so I have to sum 0.36965591924860347 + 0.10391287134268598 + 0.086884394 + ... and divide by number of intervals (I have 200 intervals in every column), and finally multiply the result by 100.
My solution would be this one:
db.collection.aggregate([
{
$set: {
analysis: {
$map: {
input: "$analysis",
in: { $objectToArray: "$$this" }
}
}
}
},
{
$set: {
analysis: {
$map: {
input: "$analysis",
in: { $first: "$$this.v" }
}
}
}
},
{ $set: { average: { $multiply: [ { $avg: "$analysis" }, 100 ] } } }
])
Mongo playground
You can use $reduce on that array,sum the values,and then divide with the number of elements and then multiply with 100
db.collection.aggregate([
{
"$addFields": {
"average": {
"$multiply": [
{
"$divide": [
{
"$reduce": {
"input": "$analysis",
"initialValue": 0,
"in": {
"$let": {
"vars": {
"sum": "$$value",
"data": "$$this"
},
"in": {
"$add": [
"$$sum",
{
"$arrayElemAt": [
{
"$arrayElemAt": [
{
"$map": {
"input": {
"$objectToArray": "$$data"
},
"as": "m",
"in": [
"$$m.k",
"$$m.v"
]
}
},
0
]
},
1
]
}
]
}
}
}
}
},
{
"$size": "$analysis"
}
]
},
100
]
}
}
}
])
You can test the code here
But this code has 1 problem, you save data in documents, and MongoDB
doesn't have a function like get(document,$$k), the new MongoDB v5.0 has a $getField but still accepts only constants no variables.
I mean we cant do in your case getField(doc,"5").
So we have the cost of converting each document to an array.

Presto Json Parsing

I have a json field(attached sample) and i need to extract the values in ProvisioningSystem path but it works only if i hardcode the array location.How can i extract the value without hardcoding ?Thanks in Advance!
Code:
TRANSFORM(CAST(JSON_EXTRACT(order_json, '$.Order.Accounts.Account') AS ARRAY), x -> JSON_EXTRACT_SCALAR(x,'$.ProvisioningSystems.ProvisioningSystem[1].SystemName'))
Json:
{
"Order":
{
"Accounts": {
"Account": [
{
"ProvisioningSystems": {},
},
{
"ProvisioningSystems": {
"ProvisioningSystem": [
{
"SystemOrderRef": "12345",
"SystemName": "Testsystem",
"SystemOrderRefType": "Provision"
}
]
},
}
]
},
}
}
}

Create Firestore database documents from Postman

I'm trying to create multiple documents at once in a Firestore collection with configuration purposes. I can create documents one by one sending from Postman:
{
"fields": {
"category":{"stringValue": "Configs"},
"order":{"integerValue":"3"}
}
}
I was wondering if there is some way to create multiple documents sending something like this:
{
"batch": [
{
"fields": {
"categoria": {
"stringValue": "Configurações"
},
"ordem": {
"integerValue": "3"
}
}
},
{
"fields": {
"categoria": {
"stringValue": "Configurações"
},
"ordem": {
"integerValue": "3"
}
}
}
]
}
Can anyone say if is it possible? If so, how can I do that?
Something like the below should work.
Although it is an "update" action is creates new documents as well if they don't exist. The document ID must be provided, it cannot be auto generated using the commit endpoint.
I was able to get my use case to work after viewing this response detailing use of commit.
API Endpoint: https://firestore.googleapis.com/v1/projects/projectID/databases/(default)/documents:commit
{
"writes": [
{
"update": {
"name": "projects/projectID/databases/(default)/documents/test/ExistingDocumentID1",
"fields": {
"comment": {
"stringValue": "Hello World!"
}
}
}
},
{
"update": {
"name": "projects/projectID/databases/(default)/documents/test/NewManualDocumentID2",
"fields": {
"comment": {
"stringValue": "Happy Birthday!"
}
}
}
}
]
}

Why is the json code wrong?

Why is the json code wrong? I know I can have multi key in XML, but it seem that json doesn't allow.
{
"BackupSettings": {
"Setting":
{
"id": "34345"
},
"Setting": {
"id": "16454"
}
}
}
Indeed, keys within an object are required to be unique in JSON. The canonical way of expressing your data in JSON would be to use an array. It could look something like the following:
{
"BackupSettings": {
"Settings": [
{
"id": "34345"
},
{
"id": "16454"
}
]
}
}
Or even:
{
"BackupSettings": [
{
"id": "34345"
},
{
"id": "16454"
}
]
}

Put Items using Json File in AWS DynamoDB using AWS CLI

While putting below JSON in dynamo DB using AWS CLI with below command:
aws dynamodb put-item --table-name ScreenList --item file://tableName.json
I am getting Parameter validation failed Exception.I have gone rigorously through AWS docs but failed to find example to insert a complicated json.Every small help is welcome.
The updated Json :
{
"itemName": {
"S": "SCREEN_LIST"
},
"productName": {
"S": "P2P_MOBITEL"
},
"screenList": {
"L": [
{
"menu": {
"L": [
{
"M": {
"menuId": {
"N": "1"
},
"menuText": {
"S": "ENG_HEADING"
},
"menuType": {
"S": "Dynamic"
}
}
}
]
},
"M": {
"screenFooter": {
"S": "F_LANGUAGE_CHANGE"
},
"screenHeader": {
"S": "H_LANGUAGE_CHANGE"
},
"screenId": {
"S": "LANGUAGE_CHANGE"
},
"screenType": {
"S": ""
}
}
}
]
}
}
It seems that you are defining complex types incorrectly. According to AWS documentation you should define a list like this:
"L": ["Cookies", "Coffee", 3.14159]
and a map should be defined like this:
"M": {"Name": {"S": "Joe"}, "Age": {"N": "35"}}
which means that a menu map should be defined like this:
"menu": {
"L": [
{
"M": {
"menuId": {"N" :"1"},
"menuText": {"S" :"PACKS_SCREEN"},
"menuType": {"S" :"Dynamic"}
}
}
]
}
Notice the "M" and "L" attributes.
You should change the rest of your JSON in a similar fashion.
You can find full JSON definition here in the Options section.
UPDATE
Now your list definition is incorrect. You have:
"screenList":{
"L":[
{
"menu":{ ... },
"M":{ ... }
}
]
}
While it should be:
"screenList":{
"L":[
{
"M":{ ... }
},
{
"M":{ ... }
},
]
}