How to store a value from JSON response in Angular JS - javascript

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.

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.

Ignore Hyphen in Javascript (Postman)

Building a RestAPI with Postman.
I have some JSON data:
{
"progress-update": {
"#type": "parallel-progress",
"job": {
"#href": "/api/space/job-management/jobs/4691268"
},
"taskId": 4691268,
"jobName": "Compare Config-4691268",
"state": "DONE",
"status": "SUCCESS",
"percentage": 100,
"data": "<![CDATA[Total requests: 3<br>InSync count : 3<br>OutOfSync count : 0<br>]]>",
"subTask": [
{
I want to pull the "state" value into an environment Variable that i can then use to determine wether to continue on to the next request or wait until the state is DONE.
The problem i'm running into is "progress-update": has a hyphen in it, causing my script to not recognize it.
var jsonData = JSON.parse(responseBody);
pm.environment.set("JobStatus", jsonData.progress-update.state);
Postman returns the following error:
There was an error in evaluating the test script: ReferenceError:
update is not defined
You should be able to access your JSON data with
var jsonData = JSON.parse(responseBody);
pm.environment.set("JobStatus", jsonData['progress-update'].state);
using the object bracket notation

Graphql query argument not working

Hello guys i want to ask, so i have JSON response like this:
{
"homeListComponents": [
{
"page": 1,
"listComponents": [
{
"componentName": "HEALTH_PRODUCT",
"componentDescription": "Showing health product component"
},
{
"componentName": "HEALTH_SERVICE",
"componentDescription": "Showing health service component"
},
{
"componentName": "OTHER_HEALTH_SERVICE",
"componentDescription": "Showing other health service component"
}
]
},
{
"page": 2,
...... //same like above
},
{
"page": 3,
...... //same like above
}
]
}
the response was successfully called in graphql using this query:
But, how can i call only page 1 ?
I already tried this query:
But the response is null.
Thank you.
You need implement your graphql resolver in server-side. It decide what result you will get.
In resolver function, your can get the arguments you passed from client.
For your question, I think you need implement a pagination
See these links:
https://graphql.org/learn/pagination/
https://www.apollographql.com/docs/react/features/pagination.html
https://github.com/mrdulin/apollo-server-express-starter/tree/master/src/pagination

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"
}
}
}
}

How can I post a container in Storage api from loopback?

I already have declared my datasource ,my model and the connector between these.
My model
{
"name": "container",
"base": "Model",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
Datasource
"storage": {
"name": "storage",
"connector": "loopback-component-storage",
"provider": "filesystem",
"root": "./server/storage"
}
My provider
{
"filesystem": {
"root": "./server/storage"
}
}
And the Connector
"container": {
"dataSource": "storage",
"public": true
}
I try posting a object like {"Object":"container1"} into path "./server/storage" but I get the following error from callback.
{
"error": {
"statusCode": 500,
"name": "TypeError",
"message": "Path must be a string. Received undefined",
"stack": "TypeError: Path must be a string. Received undefined.."
}
}
Please who can help me to find my issue? Thanks!
You can also use "name" instead of "Object" as key in your JSON object to create a new container/directory using the API.
POST /api/containers {"name":"container1"}
The way to post a container is, without using the loopback api. Create a folder that is gonna be the container into your provider path (being filesystem).
As simple as that!
If you need a programmatic way to add new containers, let's say for example you want to create a filesystem of sorts for new users. You can use the route below. "Container" is the name I called my Model, you can call yours whatever you'd like.
POST localhost:3000/api/container
Inside the body of the post request you have to have an attribute name and the value of the name can be the new container you're creating. The Strongloop/Loopback documentation, which can be found here, is not accurate and neither is the error you get back when you try to post it with their directions.
"error": {
"statusCode": 500,
"name": "TypeError",
"message": "Path must be a string. Received undefined"
}
An excerpt of the code to send a post request to create a new container is also below.
var request = require("request");
var options = {
method: 'POST',
url: 'http://localhost:3000/api/containers',
body: { name: 'someNewContainer' },
json: true
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});

Categories