AngularJS push element into existing json structure - json

I have a service that is missing an element I require in my checkbox form - productUsed = false.
How can I push that into my json so it becomes part of the model for a form page for a series of checkboxes.
The json structure is similar to the below:
"Data": [
{
"AcceptedValues": [
{
"Category": {
"Key": 2126,
"Value": "Category"
},
"ProfileOptions": [
{
"Key": 46546798,
"Value": "product Name"
},
{
"Key": 46546769,
"Value": "product Name"
},
{
"Key": 2164,
"Value": "product Name"
}
]
},
{
"Category": {
"Key": 4646789,
"Value": "Category Name"
},
"ProfileOptions": [
{
"Key": 464987946,
"Value": "Product Name"
},
{
"Key": 132465,
"Value": "Product Name"
}
]
}
]
}
]

If the value is always false (at the moment) you can directly bind it in the view : {{product.productUsed}}.
If a property does not exists, Angular sets up a new property on the scope instead (default value is false i guess)
If the data is edited later, this code will works well.

//let suppose the $scope.recordlist is the original json
$scope.UpdatedRecordlist = $scope.recordlist.map(function(obj) {
return angular.extend(obj, {productUsed:'false'});
});
// Now $scope.UpdatedRecordlist is the new updated json with productUsed element

Related

Azure data factory appending to Json

Wanted to pick your brains on something
So, in Azure data factory, I am running a set of activities which at the end of the run produce a json segment
{"name":"myName", "email":"email#somewhere.com", .. <more elements> }
This set of activities occurs in a loop - Loop Until activity.
My goal is to have a final JSON object like this:
"profiles":[
{"name":"myName", "email":"email#somewhere.com", .. <more elements> },
{"name":"myName", "email":"email#somewhere.com", .. <more elements> },
{"name":"myName", "email":"email#somewhere.com", .. <more elements> },
...
{"name":"myName", "email":"email#somewhere.com", .. <more elements> }
]
That is a concatenation of all the individual ones.
To put in perspective, each individual item is a paged data from a rest api - and all them constitute the final response. I have no control over how many are there.
I understand how to concatenate individual items using 2 variables
jsonTemp = #concat(finalJson, individualResponse)
finalJson = jsonTemp
But, I do not know how to make it all under the single roof "profiles" afterwards.
So this is a bit of a hacky way of doing it and happy to hear a better solution.
I'm assuming you have stored all your results in an array variable (let's call this A).
First step is to find the number of elements in this array. You can
do this using the length(..) function.
Then you go into a loop, interating a counter variable and
concatenating each of the elements of the array making sure you add
a ',' in between each element. You have to make sure you do not add
the ',' after the last element(You will need to use an IF condition
to check if your counter has reached the length of the array. At the
end of this you should have 1 string variable like this.
{"name":"myName","email":"email#somewhere.com"},{"name":"myName","email":"email#somewhere.com"},{"name":"myName","email":"email#somewhere.com"},{"name":"myName","email":"email#somewhere.com"}
Now all you need to do this this expression when you are pushing the
response anywhere.
#json(concat('{"profiles":[',<your_string_variable_here>,']}'))
I agree with #Anupam Chand and I am following the same process with a different Second step.
You mentioned that your object data comes from API pages and to end the until loop you have to give a condition to about the number of pages(number of iterations).
This is my pipeline:
First I have initilized a counter and used that counter each web page URL and in until condition to meet a certain number of pages. As ADF do not support self referencing variables, I have used another temporary variable to increment the counter.
To Store the objects of each iteration from web activity, I have created an array variable in pipeline.
Inside ForEach, use append variable activity to append each object to the array like below.
For my sample web activity the dynamic content will be #activity('Data from REST').output.data[0]. for you it will be like #activity('Web1').output. change it as per your requirement.
This is my Pipeline JSON:
{
"name": "pipeline3",
"properties": {
"activities": [
{
"name": "Until1",
"type": "Until",
"dependsOn": [
{
"activity": "Counter intialization",
"dependencyConditions": [
"Succeeded"
]
}
],
"userProperties": [],
"typeProperties": {
"expression": {
"value": "#equals('3', variables('counter'))",
"type": "Expression"
},
"activities": [
{
"name": "Data from REST",
"type": "WebActivity",
"dependsOn": [
{
"activity": "counter in temp variable",
"dependencyConditions": [
"Succeeded"
]
}
],
"policy": {
"timeout": "0.12:00:00",
"retry": 0,
"retryIntervalInSeconds": 30,
"secureOutput": false,
"secureInput": false
},
"userProperties": [],
"typeProperties": {
"url": {
"value": "https://reqres.in/api/users?page=#{variables('counter')}",
"type": "Expression"
},
"method": "GET"
}
},
{
"name": "counter in temp variable",
"type": "SetVariable",
"dependsOn": [],
"userProperties": [],
"typeProperties": {
"variableName": "tempCounter",
"value": {
"value": "#variables('counter')",
"type": "Expression"
}
}
},
{
"name": "Counter increment using temp",
"type": "SetVariable",
"dependsOn": [
{
"activity": "Data from REST",
"dependencyConditions": [
"Succeeded"
]
}
],
"userProperties": [],
"typeProperties": {
"variableName": "counter",
"value": {
"value": "#string(add(int(variables('tempCounter')),1))",
"type": "Expression"
}
}
},
{
"name": "Append web output to array",
"type": "AppendVariable",
"dependsOn": [
{
"activity": "Counter increment using temp",
"dependencyConditions": [
"Succeeded"
]
}
],
"userProperties": [],
"typeProperties": {
"variableName": "arr",
"value": {
"value": "#activity('Data from REST').output.data[0]",
"type": "Expression"
}
}
}
],
"timeout": "0.12:00:00"
}
},
{
"name": "Counter intialization",
"type": "SetVariable",
"dependsOn": [],
"userProperties": [],
"typeProperties": {
"variableName": "counter",
"value": {
"value": "#string('1')",
"type": "Expression"
}
}
},
{
"name": "To show res array",
"type": "SetVariable",
"dependsOn": [
{
"activity": "Until1",
"dependencyConditions": [
"Succeeded"
]
}
],
"userProperties": [],
"typeProperties": {
"variableName": "res_show",
"value": {
"value": "#variables('arr')",
"type": "Expression"
}
}
}
],
"variables": {
"arr": {
"type": "Array"
},
"counter": {
"type": "String"
},
"tempCounter": {
"type": "String"
},
"res_show": {
"type": "Array"
},
"arr_string": {
"type": "String"
}
},
"annotations": []
}
}
Result in an array variable:
You can access this array by the variable name. If you want the output to be like yours, you can use the below dynamic content.
#json(concat('{','"profile":',string(variables('res_show')),'}')))
However, if you want to store this in a variable, you have to wrap it in #string() as currently, ADF variables only supports int, string and array type only.

How to parse an array of json in scala play framework?

I have an array of json objects like this
[
{
"events": [
{
"type": "message",
"attributes": [
{
"key": "action",
"value": "withdraw_reward"
},
{
"key": "sender",
"value": "bob"
},
{
"key": "module",
"value": "distribution"
},
{
"key": "sender",
"value": "bob"
}
]
},
{
"type": "credit",
"attributes": [
{
"key": "recipient",
"value": "ross"
},
{
"key": "sender",
"value": "bob"
},
{
"key": "amount",
"value": "100"
}
]
},
{
"type": "rewards",
"attributes": [
{
"key": "amount",
"value": "100"
},
{
"key": "validator",
"value": "sarah"
}
]
}
]
},
{
"events": [
{
"type": "message",
"attributes": [
{
"key": "action",
"value": "withdraw_reward"
},
{
"key": "sender",
"value": "bob"
},
{
"key": "module",
"value": "distribution"
},
{
"key": "sender",
"value": "bob"
}
]
},
{
"type": "credit",
"attributes": [
{
"key": "recipient",
"value": "ross"
},
{
"key": "sender",
"value": "bob"
},
{
"key": "amount",
"value": "100"
}
]
},
{
"type": "rewards",
"attributes": [
{
"key": "amount",
"value": "200"
},
{
"key": "validator",
"value": "Ryan"
}
]
}
]
}
]
How to traverse through the types, check if it's type equals to rewards and then go through the attributes and verify if the validator equals to sarah and fetch the value of the key amount? Pretty new to scala and play framework. Any help would be great. Thanks
You could parse your JSON into a structure of case classes for easier handling and then extract the wanted field like so:
val json =
"""[
{"events":[
{
"type":"message","attributes":[
{"key":"action","value":"withdraw_reward"},
{"key":"sender","value":"bob"},
{"key":"module","value":"distribution"},
{"key":"sender","value":"bob"}
]},
{
"type":"credit","attributes":[
{"key":"recipient","value":"ross"},
{"key":"sender","value":"bob"},
{"key":"amount","value":"100"}
]},
{
"type":"rewards","attributes":[
{"key":"amount","value":"100"},
{"key":"validator","value":"sara"}
]}
]
},
{"events":[
{
"type":"message","attributes":[
{"key":"action","value":"withdraw_reward"},
{"key":"sender","value":"bob"},
{"key":"module","value":"distribution"},
{"key":"sender","value":"bob"}
]},
{
"type":"credit","attributes":[
{"key":"recipient","value":"ross"},
{"key":"sender","value":"bob"},
{"key":"amount","value":"100"}
]},
{
"type":"rewards","attributes":[
{"key":"amount","value":"200"},
{"key":"validator","value":"Ryan"}
]}
]
}
]
"""
case class EventWrapper(events: Seq[Event])
case class KeyValue(key: String, value: String)
case class Event(`type`: String, attributes: Seq[KeyValue])
import play.api.libs.json._
implicit val kvReads: Reads[KeyValue] = Json.reads[KeyValue]
implicit val eventReads: Reads[Event] = Json.reads[Event]
implicit val eventWrapperReads: Reads[EventWrapper] = Json.reads[EventWrapper]
val rewardAmountsValidatedBySara = Json
.parse(json)
.as[Seq[EventWrapper]]
.flatMap {
_.events.collect {
case Event(t, attributes) if t == "rewards" && attributes.contains(KeyValue("validator", "sara")) =>
attributes.collect {
case KeyValue("amount", value) => value
}
}.flatten
}
val amount = rewardAmountsValidatedBySara.head
For your example, rewardAmountsValidatedBySara would yield a List of Strings containing only the String "100". Which you could retrieve (potentially unsafe) with .head as shown above.
Normally you would not do this, as it could throw an exception on an empty List, so it would be better to use .headOption which returns an Option which you can then handle safely.
Note that the implicit Reads are Macros, which automatically translate into Code, that instructs the Play Json Framework how to read the JsValue into the defined case classes, see the documentation for more info.

Acumatica API - Creating an appointment detail marked for PO

I want to create an appointment detail line that is marked for PO. I'm setting the fields I think are relevant, but after the PUT, the MarkforPO field will reset to false. The detail record is created, but not marked for PO or any errors returned. Is there a process or specific field/value that I am missing?
Here is my Appointment PUT JSON:
{
"id": "[APPOINTMENT GUID]",
"Details": [
{
"LineRef": {
"value": "0003"
},
"LineNbr": {
"value": 3
},
"LineType": {
"value": "Non-stock item"
},
"MarkforPO": {
"value": true
},
"POSource": {
"value": "Purchase to Appointment"
},
"InventoryID": {
"value": "INVID"
},
"Description": {
"value": "INVENTORY DESC"
},
"EstimatedQty": {
"value": "1"
},
"Billable":{
"value":true
},
"UnitPrice": {
"value": "25"
},
"BillableQty": {
"value": "1"
},
"BillableAmount": {
"value": "25"
},
"UnitCost": {
"value": "25"
},
"VendorID": {
"value": "AASERVICES"
},
"VendorLocationID": {
"value": "MAIN"
}
}
]
}
The trouble was that the AppDetails.MarkforPO field was not linked to a field but mapped to FSSODet__EnablePO instead. I am not sure what this is but after adding adding another custom field directly mapped to Mark for PO I was able to modify this field through the API.

Check if keyword exists in a JSON response - Azure Logic Apps

In my Azure Logic App, I have an action to get rows from SQL database as follows.
And sample output (body) of this as follows,
{
"#odata.context": "https://logic-apis-southeastasia.azure-apim.net/apim/sql/5bb78f1b756e4b6097a8bccb6be8dae7/$metadata#datasets('virtueagintegrationssqldbsv-dev2.database.windows.net%2CLearnIntegrationDB-dev2')/tables('%5Bdbo%5D.%5BLearnEmployeeExamData%5D')/items",
"value": [
{
"#odata.etag": "",
"ItemInternalId": "ddf29856-4452-4511-a041-83a4bcf3e8fc",
"EXAMSTART": "YES",
"EXAMRESULT": "YES"
},
{
"#odata.etag": "",
"ItemInternalId": "b5a0261b-c5bf-4f14-8a87-a6acd3aaa26b",
"EXAMSTART": "YES",
"EXAMRESULT": "YES"
},
{
"#odata.etag": "",
"ItemInternalId": "7035458b-605d-431e-a352-dc91261f2a59"
},
{
"#odata.etag": "",
"ItemInternalId": "648d4c06-c3e0-45a9-b656-1aab485d12fd"
}
]
}
Is there expression to check at least one item has "EXAMSTART": "YES" from the item list "values" as shown in above response??
Ex: For above response it should output True as it's having two such items.
You can use Data Operations-> Filter Array step to get only those items with EXAMSTART: "YES":
and then use length to evaluate whether there's any array item returned from Filter Array:
code view:
"Condition": {
"actions": {},
"expression": {
"and": [
{
"greater": [
"#length(body('Filter_array'))",
0
]
}
]
},
"runAfter": {
"Filter_array": [
"Succeeded"
]
},
"type": "If"
},
"Filter_array": {
"inputs": {
"from": "#body('Get_rows_(V2)')?['value']",
"where": "#equals(item()?['EXAMSTART'], 'YES')"
},
"runAfter": {
"Get_rows_(V2)": [
"Succeeded"
]
},
"type": "Query"
},

Converting Json object in to array using lodash

I am trying to convert my JSON result to an array to bind it to my Kendo controls.
The JSON result that I am getting is as follows.
"Issues": [
{
"Id": null,
"Key": null,
"Values": [
{
"Key": "Display Name",
"Value": "Rya"
},
{
"Key": "UserName",
"Value": "RH"
},
{
"Key": "Count",
"Value": "350"
}
]
},
{
"Id": null,
"Key": null,
"Values": [
{
"Key": "Display Name",
"Value": "Mike"
},
{
"Key": "UserName",
"Value": "ML"
},
{
"Key": "Count",
"Value": "90"
}
]
}
]
The array that I needed to bind it to the Kendo control is as below.
{ "Display Name": 'Rya', "UserName" : "RH", value: 350 },
{ "Display Name": 'Mike', "UserName" : "ML", value: 90 }
i)I dont want to hardcode the strings "Display Name", "User Name", "RH". I tried v.Values[0].Key: v.Values[0].Value, but it didn't work.
ii) Also I will not know how many "key, value" pairs will be present, so I need to loop through the Values and generate the array instead of fixed
category: v.Values[0].Value,
UserName: v.Values[1].Value,
value: v.Values[2].Value,
.
.
.
score: v.values[n].value
If you're using ES6, you don't really need lodash in this case:
var r = json.Issues.map(v => ({
category: v.Values[0].Value,
value: v.Values[2].Value,
}));
http://codepen.io/cjke/pen/RprJdG?editors=0010
If want to use lodash or not-ES6, then:
var r = _.map(json.Issues, function(v) {
return {
category: v.Values[0].Value,
value: v.Values[2].Value,
}
});