ASP JSON: Multidimensional JSON array - arrays

How should I access the "games" array within this JSON? I'm using the class from http://aspjson.com.
{
"period":[
{
"period_id":"1774",
"start_time_epoch":1431126300,
"games":[
{
"home_team":"WSH",
"away_team":"ATL"
}
]
}
]
}
Method of access:
today = date
For Each key In oJSON.data("period")
Set this = oJSON.data("period").item(key)
periodID = this.item("period_id")
periodDate = FormatDateTime(DateAdd("s", this.item("start_time_epoch"), "01/01/1970 00:00:00"),2)
----Ideally, this.item("games").item("home_team") would have worked.----
if cstr(today) = periodDate then
response.write periodID & " - " & periodDate & "<br/> - " & this.item("games").item("start_time")
end if
Next

Your JSON is not valid: http://jsonformatter.curiousconcept.com/
This would be valid:
{
"period":[
{
"period_id":"1774",
"start_time_epoch":1431126300,
"games":[
{
"home_team":"WSH",
"away_team":"ATL"
}
]
}
]
}
To access the home team you can use
Response.Write( oJSON.data("period")(0)("games")(0)("home_team"))

Related

React Amplify/Datastore- cannot get delete record to delete

In react, I have the following code which is running offline. I obviously found it.:
async function deleteMessage() {
toDelete = await DataStore.query(Message, c => c.id("eq","4faebf3b-f89a-4e34-978a-0fea611897e0"))
alert("toDelete: " + !toDelete ? toDelete : toDelete.length )
if (!toDelete || (toDelete !== undefined && toDelete.length == 0))
{
alert("Nothing to delete")
return
}
alert("Deleting " + toDelete.length + " records.")
alert("id for 0 = " + toDelete[0].id + " title: " + toDelete[0].title + " color: " + toDelete[0].color + " createdAt: " + toDelete[0].createdAt)
await DataStore.delete(toDelete);
alert("after delete")
// updateFormState(initialState)
}
toDelete has all the fields correct and is being displayed in the alert before the actual delete, so toDelete should be the correct model.
The one alert is saying: Deleting 1 records.
The next alert says: id for 0 = 4faebf3b-f89a-4e34-978a-0fea611897e0 title: aaasdfasdf color: #6c2f2f createdAt: undefined
But it never returns from the delete and I am getting the error:
Unhandled Rejection (Error): Object is not an instance of a valid model
DataStore.<anonymous>
C:/Projects/React/src/datastore/datastore.ts:897
894 | sortPredicate = ModelSortPredicateCreator.createFromExisting(modelDefinition, paginationProducer.sort);
895 | }
896 | return {
> 897 | limit: limit,
| ^ 898 | page: page,
899 | sort: sortPredicate,
900 | };
View compiled
What am I missing?
Also, the model is:
type Message #model {
id: ID!
title: String!
color: String
createdAt: String
}
Thanks.
Figured it out.
Change toDelete = await DataStore.query(Message, c => c.id("eq","4faebf3b-f89a-4e34-978a-0fea611897e0"))
to
toDelete = await DataStore.query(Message, "4faebf3b-f89a-4e34-978a-0fea611897e0"))
The first example returns an array of objects.
The second example returns an object.
The DataStore.delete() expects an object not an array of objects. I was passing an array of objects.
DataStore.delete(toDelete) would work with the 2nd example but not the first since that was an object.
If I am using the first example would have to use:
DataStore.delete(toDelete[0])

terraform database creation in athena

I am trying to create a database using terraform and this seems very complicated for a poor query...
Could you help me, please?
I have tried null_resource with local-exec and data "external" Python...
I think I am looking the wrong way
ex which doesn't works in terraform 0.12
resource "null_resource" "create-endpoint" {
provisioner "local-exec" {
query = <<EOF
{
CREATE EXTERNAL TABLE `dashboard_loading_time`(
`timestamp_iso` string,
`app_identification` struct<service:string,app_name:string,app_type:string,stage:string>,
`user` struct<api_gateway_key:struct<id:string,name:string>,mashery_key:struct<id:string,name:string>,employee:struct<id:string,name:string>>,
`action` struct<action_type:string,path:string>,
`result` struct<status:string,http_status:string,response:struct<response:string>>)
PARTITIONED BY (
`year` int)
ROW FORMAT SERDE
'org.openx.data.jsonserde.JsonSerDe'
STORED AS INPUTFORMAT
'org.apache.hadoop.mapred.TextInputFormat'
OUTPUTFORMAT
'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
's3://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/dev'
}
EOF
command = "aws athena start-query-execution --query-string "query""
}
}
I would like to find the simplest way to do this using terraform.
If you wanna make it for athena, need to make glue resources.
try below code with terraform.
variable "service_name" {
default = "demo-service"
}
variable "workspace" {
default = "dev"
}
variable "columns" {
default = {
id = "int"
type = "string"
status = "int"
created_at = "timestamp"
}
}
resource "aws_glue_catalog_database" "athena" {
name = "${var.service_name}_db"
}
resource "aws_glue_catalog_table" "athena" {
name = "${var.service_name}_logs"
database_name = "${aws_glue_catalog_database.athena.name}"
table_type = "EXTERNAL_TABLE"
parameters = {
EXTERNAL = "TRUE"
}
storage_descriptor {
location = "s3://${var.service_name}-${var.workspace}-data-pipeline/log/"
input_format = "org.apache.hadoop.mapred.TextInputFormat"
output_format = "org.apache.hadoop.hive.ql.io.IgnoreKeyTextOutputFormat"
ser_de_info {
name = "jsonserde"
serialization_library = "org.openx.data.jsonserde.JsonSerDe"
parameters = {
"serialization.format" = "1"
}
}
dynamic "columns" {
for_each = "${var.columns}"
content {
name = "${columns.key}"
type = "${columns.value}"
}
}
}
partition_keys {
name = "year"
type = "string"
}
partition_keys {
name = "month"
type = "string"
}
partition_keys {
name = "day"
type = "string"
}
partition_keys {
name = "hour"
type = "string"
}
}
refer to this repository : aws-serverless-data-pipeline-by-terraform
resource "aws_glue_catalog_table" "aws_glue_catalog_table" {
name = "mytable"
database_name = aws_glue_catalog_database.aws_glue_catalog_database.name
table_type = "EXTERNAL_TABLE"
parameters = {
"classification" = "json"
}
storage_descriptor {
location = "s3://mybucket/myprefix"
input_format = "org.apache.hadoop.mapred.TextInputFormat"
output_format = "org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat"
ser_de_info {
name = "myserdeinfo"
serialization_library = "org.openx.data.jsonserde.JsonSerDe"
parameters = {
"paths" = "jsonrootname"
}
}
columns {
name = "column1"
type = "array<struct<resourcearn:string,tags:array<struct<key:string,value:string>>>>"
}
}
partition_keys {
name = "part1"
type = "string"
}
partition_keys {
name = "part2"
type = "string"
}
}

How do I organize the data of a json obtained from a server?

I'm making an application in unity that connects to a database and I need to organize the information obtained from it, I use the following coruotine to get the information:
private IEnumerator GetUsers(string url)
{
using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
{
// Request and wait for the desired page.
yield return webRequest.SendWebRequest();
if (webRequest.isNetworkError)
{
Debug.Log(": Error: " + webRequest.error);
}
else
{
Debug.log("DATA:\n" + webRequest.downloadHandler.text);
}
}
}
and then I would like the data I receive (names, surnames, code, password ... etc) to organize it to print them or to later use them depending on what I need, thank you very much in advance.
and tried to use a serializable class to save my data but I only work with one (and when with my own data not from the server lol),I have also tried to make a vector of type people to save more than one data and it did not work either
:C or rather I did not know how to make it work
[System.Serializable]
public class People
{
public string names, surnames, cedula, password, telephone, address, code,
email;
}
[System.Serializable]
public class PeopleList
{
public People[] users;
}
public class GetSendDate : MonoBehaviour
{
private IEnumerator GetUsers(string url)
{
using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
{
// Request and wait for the desired page.
yield return webRequest.SendWebRequest();
if (webRequest.isNetworkError)
{
Debug.Log(": Error: " + webRequest.error);
}
else
{
PeopleList users = new People();
string json = webRequest.downloadHandler.text;
users = JsonUtility.FromJson<PeopleList>(json);
Debug.Log(users.user[0].names);
Debug.Log(users.user[0].surnames);
Debug.Log(users.user[0].cod);
}
}
}
}
this is the response of webRequest.downloadHandler.text
[{"userID": 1, "idRole": 1, "email": "ada#gmail.com", "password": "$ 2a $ 10 $ YVXachXCaPBj9vDo.d4itO4vghtCvSMfrmeHCGJqJ6rSneM / hJsPy", "names": "Juanota Rosadita" , "surnames": "sapoton", "cedula": "123", "telefono": "789", "direccion": "147", "code": "258"}, {"idUsuario": 7, " idRole ": 1," email ":" vic#gmail.com "," password ":" $ 2a $ 10 $ JA1rszAgVK52OnWoOWDXneQcVUHwWBi2Di9o2z7kMqrWPyrjGoTnO "," names ":" victor "," Surnames ":" giron "," cedula ":" 1085 "," telefono ":" 313 "," direccion ":" yoquese "," code ":" 217 "}, {" idUsuario ": 8," idRole ": 1," email ":" juanos # gmail. com "," password ":" $ 2a $ 10 $ 6EAy2e7dXASx2MPDA3vtW.heYuM1wsaEtFmA4Lb6BD0RCTJvm / HSe "," names ":" Juanito "," Surnames ":" Alcachofa "," cedula ":" 789 "," telefono ":" 31358964 " , "address": "123", "code": "753"}]
I get the following error when I run the function in unity:
ArgumentException: JSON must represent an object type.
UnityEngine.JsonUtility.FromJson (System.String json, System.Type type) (at C:/buildslave/unity/build/Modules/JSONSerialize/Public/JsonUtility.bindings.cs:42)
UnityEngine.JsonUtility.FromJson[T] (System.String json) (at C:/buildslave/unity/build/Modules/JSONSerialize/Public/JsonUtility.bindings.cs:30)
GetSendDate+d__6.MoveNext () (at Assets/Scripts/GetSendDate.cs:73)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17)
Can you send a capture "Debug.log" of your response webRequest.downloadHandler.text?
Because it is very likely that you are trying to transform the entire response, including the header. you only have to transform the response data to json.
You can use Newton Soft Json Utility for parsing.
Just download DLL file and put it in your Assets/Plugins folders
You can download from:
https://www.nuget.org/packages/Unity.Newtonsoft.Json/
using Newtonsoft.Json;
private IEnumerator GetUsers(string url)
{
using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
{
yield return webRequest.SendWebRequest();
if (webRequest.isNetworkError)
{
Debug.Log(": Error: " + webRequest.error);
}
else if (m_Request.isHttpError)
{
Debug.Log(" Server Not Responding ");
}
else
{
var response = JObject.Parse(m_Request.downloadHandler.text);
users = JsonConvert.DeserializeObject<PeopleList>(response.SelectToken("data"))
}
}
}

Handle Multiple Response Data with REST API

I have issue in handling json multiple data. I am not getting multiple data and if I use "array" then also it creates single record with null values in database.
The Backend code for saving data
{
$pickup = new PickupData();
$pickup->pickup_person = $request->pickup_person;
$pickup->office_city = $request->office_city;
$pickup->office_state = $request->office_state;
$pickup->office_pincode = $request->office_pincode;
$pickup->pickup_email = $request->pickup_email;
$pickup->preferred_start_time = $request->preferred_start_time;
$pickup->preferred_end_time = $request->preferred_end_time;
$pickup->mobile = $request->mobile;
$pickup_date = Carbon::parse($request->pickup_date)->format('d-m-Y');
$pickup->pickup_date = $pickup_date;
$saved = $pickup->save();
}
I need response like and it should be saved into database
[
{
"pickup_person":"SanPune6",
"city":"pune",
"office_address":"viman nagar",
"office_city":"Pune",
"office_state":"MH",
"office_pincode":"121212",
"pickup_email":"office#gmail.com",
"preferred_start_time":"12:38:29",
"preferred_end_time":"13:02:33",
"mobile":"9090909090",
"pickup_date":"12-04-2019"
},
{
"pickup_person":"SanPune6",
"city":"pune",
"office_address":"viman nagar",
"office_city":"Pune",
"office_state":"MH",
"office_pincode":"121212",
"pickup_email":"office#gmail.com",
"preferred_start_time":"12:38:29",
"preferred_end_time":"13:02:33",
"mobile":"9090909090",
"pickup_date":"12-04-2019"
}
]
create resource sth like this
php artisan make:resource PickupResourceCollection
create another resource sth like
php artisan make:resource PickupResource
in your index method of PickupController do sth like this
use use App\Http\Resources\PickupResourceCollection;
...
public function index()
{
$pickups = Pickup::all();
return response()->json(new PickupResourceCollection($pickups),200);
}
...
in PickupResourceCollection do sth like this
....
public function toArray($request)
{
return [
'data' => $this->collection->transform(function ($pickups) {
return new PickupResource($pickups);
})
];
}
....
in PickupResource do sth like this
....
public function toArray($request)
{
return [
"pickup_person" =>$this->pickup_person,
"city" =>$this->city,
"office_address" =>$this->office_address,
"office_city" =>$this->office_city,
"office_state" =>$this->office_state,
"office_pincode" =>$this->office_pincode,
"pickup_email" =>$this->pickup_email,
"preferred_start_time"=>$this->preferred_start_time,
"preferred_end_time" =>$this->preferred_end_time,
"mobile" =>$this->mobile,
"pickup_date" =>"$this->pickup_date
];
}
....
thats it :)
Yes you can save by as an array.
$pickup = PickupData::create($request->all()) // to save all request data
$pickup = PickupData::create($request->except(['var1', 'var2'])) // to save all request data except var1 & var2
$pickup will return only newly inserted row.
$pickupDetails = [
{
"pickup_person":"SanPune6",
"city":"pune",
"office_address":"viman nagar",
"office_city":"Pune",
"office_state":"MH",
"office_pincode":"121212",
"pickup_email":"office#gmail.com",
"preferred_start_time":"12:38:29",
"preferred_end_time":"13:02:33",
"mobile":"9090909090",
"pickup_date":"12-04-2019"
},
{
"pickup_person":"SanPune6",
"city":"pune",
"office_address":"viman nagar",
"office_city":"Pune",
"office_state":"MH",
"office_pincode":"121212",
"pickup_email":"office#gmail.com",
"preferred_start_time":"12:38:29",
"preferred_end_time":"13:02:33",
"mobile":"9090909090",
"pickup_date":"12-04-2019"
}
];
$pickupDetails = $request->all();
foreach($pickupDetails as $pickup){
Pickup::create($pickup);
}
return response()->json("success",200);
if it helps you upvote me :)

PostgresSql + Nodejs (ClaudiaJS) : How to cast array of string to array of timestamp

I am writing API which insert into a table with multiple rows, I am using UNNEST to make it work.
What I have done:
in js file:
api.post(PREFIX + '/class/insert', function (request) {
var db = pgp(dbconnect);
//Params
var data = request.body; //should be an array
var classes = [];
var starts = [];
var ends = [];
for (var i = 0; i < data.length; i++) {
classes.push(data[i].class_id);
starts.push(data[i].timestamp_start);
ends.push(data[i].timestamp_end);
}
const PQ = require('pg-promise').ParameterizedQuery;
var sql =
"INSERT INTO sa1.class(class_id, timestamp_start, timestamp_end) " +
"VALUES( "+
"UNNEST(ARRAY" + JSON.stringify(classes).replace(/"/g, "'") + "), " +
"UNNEST(ARRAY" + JSON.stringify(starts).replace(/"/g, "'") + "), " +
"UNNEST(ARRAY" + JSON.stringify(ends).replace(/"/g, "'") + ")"
const final_sql = new PQ(sql);
return db.any(final_sql)
.then(function (data) {
pgp.end();
return 'successful';
})
.catch(function (error) {
console.log("Error: " + error);
pgp.end();
});
}
Request body
[{
"class_id":"1",
"timestamp_start":"2017-11-14 14:01:23.634437+00",
"timestamp_end":"2017-11-14 15:20:23.634437+00"
}, {
"class_id":"2",
"timestamp_start":"2017-11-14 15:01:23.634437+00",
"timestamp_end": "2017-11-14 16:20:23.634437+00"
}]
When I run api in postman, I get the error is:
column "timestamp_start" is of type timestamp with time zone but
expression is of type text
Issue is obviously from ARRAY of string that I used in sql, my question is how to create ARRAY of timestamp for UNNEST, or any suggestion are appreciated.
Thanks
Never initialize the database inside the handler, see: Where should I initialize pg-promise
Never call pgp-end() inside HTTP handlers, it destroys all connection pools.
Use static ColumnSet type to generate multi-insert queries.
Do not return from db.any, there is no point in that context
You must provide an HTTP response within an HTTP handler
You are providing a confusing semantics for column class_id. Why is it called like that and yet being converted into a timestamp?
Never concatenate objects with strings directly.
Never concatenate SQL strings manually, it will break formatting and open your code to SQL injection.
Use Database methods according to the expected result, i.e. none in your case, and not any. See: https://github.com/vitaly-t/pg-promise#methods
Initialize everything needed only once:
const db = pgp(/*connection*/);
const cs = new pgp.helpers.ColumnSet([
'class_id',
{
name: 'timestamp_start',
cast: 'timestamp'
},
{
name: 'timestamp_end',
cast: 'timestamp'
}
], {table: {table: 'class', schema: 'sa1'}});
Implement the handler:
api.post(PREFIX + '/class/insert', request => {
const sql = pgp.helpers.insert(request.body, cs);
db.none(sql)
.then(data => {
// provide an HTTP response here
})
.catch(error => {
console.log('Error:', error);
// provide an HTTP response here
});
}
Many thanks to #JustMe,
It worked after casting array
var sql =
"INSERT INTO sa1.class(class_id, timestamp_start, timestamp_end) " +
"VALUES( "+
"UNNEST(ARRAY" + JSON.stringify(classes).replace(/"/g, "'") + "), " +
"UNNEST(ARRAY" + JSON.stringify(starts).replace(/"/g, "'") + "::timestamp[]), " +
"UNNEST(ARRAY" + JSON.stringify(ends).replace(/"/g, "'") + "::timestamp[])"

Resources