AWS - import JSON file to load Dynamo table - javascript

I have a json file that I want to use to load my Dynamo table in AWS.
In the AWS console, there is only an option to create one record at a time. Not good: )
Essentially my .JSON file is an array of objects which hold the data for each column in the table
ie:
{
"Column1": "Column1 Value",
"Column2": "Column2 Value",
"Column3": "Column3 Value",
"Column4": "Column4 Value",
},
Is there any way to do this via AWS console and importing my json file, or do I have to use AWS JS SDK to programmatically do this ??

The answer from E.J. Brennan looks correct, for a single record, but it doesn't answer the original question (which needs to add an array of records).
For this, the command is
aws dynamodb batch-write-item --request-items file://aws-requests.json
But, you'll need to make a modified JSON file, like so (note the DynamoDB JSON that specifies data types):
{
"YourTableName": [
{
"PutRequest": {
"Item": {
"Column1": { "S": "Column1 Value" },
"Column2": { "S": "Column2 Value" },
"Column3": { "S": "Column3 Value" },
"Column4": { "S": "Column4 Value" },
}
}
},
{
"PutRequest": {
"Item": {
"Column1": { "S": "Column1 Value" },
"Column2": { "S": "Column2 Value" },
"Column3": { "S": "Column3 Value" },
"Column4": { "S": "Column4 Value" },
}
}
}
]
}

You don't need to use the API. You could use the AWS-CLI instead, i.e:
aws dynamodb put-item --table-name MusicCollection --item file://item.json --return-consumed-capacity TOTAL
but you may need to tweak your JSON format a bit.
More examples and documentation here:
https://docs.aws.amazon.com/cli/latest/reference/dynamodb/put-item.html

I used boto3 in python to load the data
import boto3
import json
dynamodbclient=boto3.resource('dynamodb')
sample_table = dynamodbclient.Table('ec2metadata')
with open('/samplepath/spotec2interruptionevent.json', 'r') as myfile:
data=myfile.read()
# parse file
obj = json.loads(data)
#instance_id and cluster_id is the Key in dynamodb table
response=sample_table.put_item(
Item={
'instance_id': instanceId,
'cluster_id': clusterId,
'event':obj
}
)
Here is a sample for javascript:
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Js.02.html#GettingStarted.Js.02.02

The code above doesn't read in individual JSON objects, if you wanted to do that from a JSON file with multiple objects:
import boto3
import json
dynamodbclient=boto3.resource('dynamodb')
sample_table = dynamodbclient.Table('ec2metadata')
with open('/samplepath/spotec2interruptionevent.json', 'r') as myfile:
data=myfile.read()
# parse file
objects = json.loads(data)
#instance_id and cluster_id is the Key in dynamodb table
for object in objects:
instance_id = object["instance_id"]
cluster_id = object["cluster_id"]
sample_table.put_item=(item=object)

Related

How to note data source in a data JSON file?

I have a few data JSON files similar to this and I want to include a line to note the sources as simple as //source: Cambodia Department of Injustice but JSON files should contain only data.
Should should it be done?
If you can change the data structure slightly, you could add a level for metadata:
{
"metadata": {
"source": "Cambodia Department of Justice",
"source-date": "2015-12-15",
"note": "Ha ha made you look"
},
"countries": {
"USA": { some data }
"Canada": { Some data }
}
}
This is cleaner than use a fake "non-data" country.

pass json in process variables in camunda process

I am trying to pass json payload in variables as value to start a process definition using engine-rest api as below:-
API:
http://localhost:8080/engine-rest/process-definition/processService:1:9459dbe9-6b2c-11e8-b9e8-28d2447c697a/start
Body :
{
"variables": {
"payload": {
"value": {
"mode": "email",
"meta": [{
"key": "topic",
"value": "weather"
}, {
"key": "qos",
"value": "2"
}]
},
"type": "Json"
}
}
}
but it is giving 400 BAD REQUEST with below error:-
Must provide 'null' or String value for value of SerializableValue type 'Json'.
Also i have used a expression in my BPMN process to fetch a key-value pair like below, it also throwing me error :-
${S(payload).prop("mode").stringValue() == 'email'}
Now working steps:-
when i try to send body json payload in string format then it works fine.
API:
http://localhost:8080/engine-rest/process-definition/processService:1:9459dbe9-6b2c-11e8-b9e8-28d2447c697a/start
Body:
{
"variables": {
"payload": {
"value": "{\"mode\": \"email\",\"meta\": [{\"key\": \"topic\",\"value\": \"weather\"},{\"key\": \"qos\",\"value\": \"2\"}]}",
"type": "String"
}
}
}
same java code i am using here to fetch json payload-
public void notify(DelegateExecution delegateProcessExecution) throws Exception {
Object notificationPayload =
delegateProcessExecution.getVariable("payload");
if (null != notificationPayload) {
String notifyPayload = notificationPayload.toString();
JSONObject inputJson = new JSONObject(notifyPayload);
}
// ...
}
So i want this payload as json for whole process so that i don't need to convert it to string as above working example.
You should only change the type to "json", example:
{
"variables": {
"broker": {
"value": "{\"name\":\"Broker Name\"}",
"type": "json"
}
}
}
This is by design in the rest engine API, they support other data formats, too, so it has to be an escaped JSON String, see https://app.camunda.com/jira/browse/CAM-9617.
The solution is to pass an escaped JSON String as value, as you have pointed out above. One can also use "type": "Object" if the engine has a Jackson Java Bean on the classpath that matches the given value. You supply the bean type name in a valueInfo object:
https://docs.camunda.org/manual/7.10/reference/rest/process-definition/post-start-process-instance/#request-body
For example:
{
"variables": {
"payload": {
"value": "{\"mode\": \"email\",\"meta\": [{\"key\": \"topic\",\"value\": \"weather\"},{\"key\": \"qos\",\"value\": \"2\"}]}",
"type": "String",
"valueInfo": {
"objectTypeName": "my.own.BeanWithModeAndMetaProps",
"serializationDataFormat": "application/json"
}
}
}
}

JSON, node.js: access classes via its name (String)

Here's my situation, I have a JSON that looks somewhat like this:
{
"items": [{
"type": "condition",
"data": {
"type": "comparison",
"value1": {
"source": "MyType1",
"component": "Attribute1"
},
"value2": {
"source": "MyType2",
"component": "Attribute2"
},
"operator": "gt"
}
},
{
"type": "then",
"data": {
"result": "failed",
"message": "value1 is too high"
}
}
]
}
and would want it to translate to:
if (MyType1.Attribute1 > MyType2.Attribute2) {
result = "failed";
console.log("value1 is too high");
}
Now my problem is, I don't know how I would translate the entries of value1 and value2 to actual code, or rather, how I could access the Object MyType1(maybe through something like getAttribute("MyType1")).
Since I am going to have a whole bunch of sources which each have different components, I cant really write a huge dictionary. Or I would like to avoid it.
The goal is to allow creating if - then - statements via some interactive UI, and I figured it'd be best to save that code as .json files. (Think rule management system).
So, TL,DR, How would I access a Class Attribute this.MyType, if I only have a String MyType to go from? And how would I access the value this.MyType.MyValue, if I get another String MyValue?
Thanks in advance.
Edit:
I'd really like to avoid using eval, for obvious reasons. And if I have to - I guess I would need to create Dictionaries for possible JSON Values, to validate the input?
You need some kind of parser. At first we need some way to store variables and maybe flags:
const variables = {};
var in = false;
Then we go through the code and execute it:
for(const command of items){
switch( command.type ){
case "condition":
//...
case "then":
//...
}
}
To access a variable we can simply do
var current = variables[ identifier ];
To store its the other way round:
variables[ identifier ] = current;

How to store a value from JSON response in Angular JS

This is my JSON Response of my Rest API.
{"ListClientResponse": {
"Header": {"CMMHeader": {"CorrelationId": "cmm:CorrelationId"}},
"Result": {
"ResponseCode": "CM-N-0000",
"ResponseMessage": "No errors and warnings."
},
"ListClient": {"Client": [
{
"OrganizationId": 523,
"OrganizationName": "OrgX1518521.com",
"OrganizationDomain": "X1518521.com"
],
"RoleId": "AdminRole",
"RoleName": "Admin"
}
},
I want to save Organization Id and OrganizationDomain from my JSON Response in a variables or something so that I can use these values later in my all JSON requests.
This is my JSON request.
var myCreateUserRequest = {
"CreateUserRequest": {
"Header": {
"CMMHeader": {
"CorrelationId": 5454354}},
"ClientContext": {
"OrganizationId": **theOrgId,**
"OrganizationDomain": **theDomain,**
},
"User": {
"UserName": aUser.Username,
"UserPassword": aUser.Password,
"UserStatus": "Active",
"RoleId": "Member"
}
}
}
In my JSON request, inside the field of OrganizationId and OrganizationDomain I want to pass that organizationID value and OrganizationDomain value that I have saved from my JSON Response in a varibale. I want to save it in a way so that organizationId and OrganizationDomain can be accessible in my whole ANGULAR JS project and I can pass it in my any JSON request. How can I do that. Please tell me any suggestion.
Best practice in this case tells to create a service. Becouse services are singleton in Angular, you can set a vale and inject in yours services/controllers and have access to the value.

how to test a json file with json schema file

I'm newbie to json. I'm learning more things in Json schema but I stood helpless in testing my user.json file against json-schema.json file. Please note I need to test with a javascript variable which should return either true or false to process further. Hereby I pasted my files.
json-schema.json
{
"description": "Any validation failures are shown in the right-hand Messages pane.",
"type": "object",
"properties": {
"foo": {
"type": "number"
},
"bar": {
"type": "string",
"enum": [
"a",
"b",
"c"
]
}
}
}
user.json
{
"foo": 12345,
"bar": "a"
}
When I tested the above code in http://jsonschemalint.com/#/version/draft-05/markup/json IT say's user.json is in right format. But I need to test locally
Thanks in advance.
You can use one of JSON schema validators.
Example of using one of these libraries, ajv:
import Ajv from 'ajv';
import schema from 'schema.json';
import data from 'data.json';
function isValid(schema, data) {
const ajv = new Ajv();
const valid = ajv.validate(schema, data);
if (!valid) {
console.log(ajv.errors);
return false;
}
return true;
}

Categories