How to get values from object as an array - arrays

We need to convert the object to an Array of objects and do map on the field but the field is incremental like field1, field2 which is where we got stuck.
I tried the below code:
output application/json
---
payload.main.field map(value) -> {
"name": value.name,
"age": value.age,
"location": value.location[0].country
}
Input:
{
"main": {
"field1": {
"name": "value",
"age": 20,
"address": {
"location": [
{
"country": "US",
"zipcode": 1234
},
{
"country": "US",
"zipcode": 1234
}
]
}
},
"field2": {
"name": "pqr",
"age": 23,
"address": {
"location": [
{
"country": "CA",
"zipcode": 1235
},
{
"country": "US",
"zipcode": 1234
}
]
}
},
"field3": {
"name": "abc",
"age": 22,
"address": {
"location": [
{
"country": "BU",
"zipcode": 1236
},
{
"country": "US",
"zipcode": 1234
}
]
}
}
}
}
For the above Input, Below is the expected response.
Expected Output:
{
"main": [
{
"name": "value",
"age": 20
"location": "US"
},
{
"name": "pqr",
"age": 23
"location": "CA"
},
{
"name": "abc",
"age": 22
"location": "BU"
}
]
}
For location, it will be like location[0].country when the array size is not 0 and country not null.

output application/json
---
main : payload.main pluck $ map {
"name": $.name,
"age": $.age,
"location": if( sizeOf($.address.location) !=0) $.address.location[0].country else "N/A"
}

Related

Extracting an array from within an object

I have the following JSON object from which I'd like to extract a specific property
{
"gender": "male",
"age": 36,
"profession": "teacher",
"hobbies": [
{
"id": 28,
"name": "gardening"
},
{
"id": 878,
"name": "Football"
},
{
"id": 35,
"name": "Reading"
},
{
"id": 10751,
"name": "Socialising"
}
],
"Country": "Sweden",
"id": 4542236,
}
Lets say I wanted to create a new object that hold only the "hobbies" property with all the values like this
{
"hobbies": [
{
"id": 28,
"name": "gardening"
},
{
"id": 878,
"name": "Football"
},
{
"id": 35,
"name": "Reading"
},
{
"id": 10751,
"name": "Socialising"
}
]
}
Since the JSON is coming from an external API I have no control on future positioning (index) of any properties

GROOVY transform single array into deep nested array

I have an array as the source. I want to transform source into result by using Groovy.
I don't see any similar question. That's why I post here.
I tried to get the first member in the family and put all other members into a subList with this code but it failed
source.each{ family -> family.each{
member -> member.get(0).collate(1,family.size()-1)
}
}
source:
[
[{
"id": "0001",
"role": "parent",
"age": 30
},
{
"id": "0002",
"role": "child",
"age": 1
},
{
"id": "0003",
"role": "child",
"age": 3
}
],
[{
"id": "0004",
"role": "parent",
"age": 31
},
{
"id": "0005",
"role": "child",
"age": 5
}
]
]
result:
[{
"id": "0001",
"role": "parent",
"age": 30,
"children": [{
"id": "0002",
"role": "child",
"age": 1
},
{
"id": "0003",
"role": "child",
"age": 3
}
]
},
{
"id": "0004",
"role": "parent",
"age": 31,
"children": [{
"id": "0005",
"role": "child",
"age": 5
}]
}]
You can shape the data by adding the "parent" map with a new map only containing the children (+ in groovy does that merge). E.g.:
def data = new groovy.json.JsonSlurper().parseText('[[{ "id": "0001", "role": "parent", "age": 30 }, { "id": "0002", "role": "child", "age": 1 }, { "id": "0003", "role": "child", "age": 3 } ], [{ "id": "0004", "role": "parent", "age": 31 }, { "id": "0005", "role": "child", "age": 5 }]]')
println(data.collect{ groups ->
// XXX
groups.find{ it.role=="parent" } + [children: groups.findAll{it.role=="child"}]
})
// => [[id:0001, role:parent, age:30, children:[[id:0002, role:child, age:1], [id:0003, role:child, age:3]]], [id:0004, role:parent, age:31, children:[[id:0005, role:child, age:5]]]]

AngularJS multiple Scope needed for Autocomplete

I have an array structure like this.
[
{
"id": "1",
"name": "John",
"city": "NY"
},
{
"id": "2",
"name": "Gerold",
"city": "LA"
},
{
"id": "3",
"name": "Stuart",
"city": "Boston"
}
]
I need $scope like below for my autocomplete search.
$scope.name=["john","Gerold","Stuart"];
$scope.city=["NY","LA","Boston"];
can anyone help to get this using angularjs controller.
Thanks in Advance.!
Use MAP
$scope.users = [
{
"id": "1",
"name": "John",
"city": "NY"
},
{
"id": "2",
"name": "Gerold",
"city": "LA"
},
{
"id": "3",
"name": "Stuart",
"city": "Boston"
}
];
$scope.cities = $scope.users.map(function(obj){
return obj.city;
});
console.log($scope.cities);
You can also create a helper function that would do that for you and you don't have to define a map per function that you want, and you do it in just one run (hence just a bit faster)
Sample here ;)
var myArray = [
{
"id": "1",
"name": "John",
"city": "NY"
},
{
"id": "2",
"name": "Gerold",
"city": "LA"
},
{
"id": "3",
"name": "Stuart",
"city": "Boston"
}
]
function toScope(scopedPropertieNames, array) {
scopedPropertieNames.forEach(function(propertyName) {
if (! $scope[propertyName]) {
$scope[propertyName] = []
}
});
array.forEach(function (objecInArray) {
scopedPropertieNames.forEach(function(propertyName) {
$scope[propertyName].push(objecInArray[propertyName])
})
});
}
toScope(['name', 'city'], myArray);
console.log($scope) //{name: Array[3], city: Array[3]}
You can use MAP..
$scope.YourBigArray = [{
"id": "1",
"name": "John",
"city": "NY"
}, {
"id": "2",
"name": "Gerold",
"city": "LA"
}, {
"id": "3",
"name": "Stuart",
"city": "Boston"
}];
$scope.names = $scope.YourBigArray.map(function(object) {
return object.name;
});
$scope.cities = $scope.YourBigArray.map(function(object) {
return object.city;
});
You can do a filter to use unique things in array of names and cities..
function filterForDuplicate(things) {
return things.filter(function(item, pos) {
return things.indexOf(item) === pos;
});
}

Angular Differences Between Two Models

I have an angular application which uses a significantly large shared model. Currently, when a user presses save the entire model is posted to a RESTful service. Ideally I would only like to post the fields that have changed. Since this is a shared model I do not have access to form validation states such as dirty/pristine etc. The idea I can think of is to have two models, the original and the modified and compare these.
Original Model
{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"id": "123",
"number": "212 555-1234"
},
{
"id": "456",
"number": "646 555-4567"
},
{
"id": "789",
"number": "123 456-7890"
}
],
"children": [],
"spouse": null
}
Changed Model
{
"firstName": "Jane",
"lastName": "Smith",
"isAlive": true,
"age": 50,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"id": "123",
"number": "1234567890"
},
{
"id": "456",
"number": "646 555-4567"
},
{
"id": "789",
"number": "123 456-7890"
}
],
"children": [],
"spouse": null
}
Data Posted - This is what I need!
{
"firstName": "Jane",
"age": 50,
"phoneNumbers": [
{
"id":"123",
"number": "1234567890"
}
]
}
How can I achieve this? I need the changed fields including any fields called id!
You'll need something like this. Working Fiddle: https://jsfiddle.net/jyyyLaot/
function filter(obj1, obj2) {
var result = {};
for(key in obj1) {
if(obj2[key] != obj1[key]) result[key] = obj2[key];
if(typeof obj2[key] == 'array' && typeof obj1[key] == 'array')
result[key] = arguments.callee(obj1[key], obj2[key]);
if(typeof obj2[key] == 'object' && typeof obj1[key] == 'object')
result[key] = arguments.callee(obj1[key], obj2[key]);
}
return result;
}

Get Value After Payment

I have the following object. How do I get the email value from it?
{
"payment_method": "paypal",
"payer_info": {
"email": "example#domain.com",
"first_name": "example",
"last_name": "xxx",
"payer_id": "12313213",
"shipping_address": {
"recipient_name": "Example",
"id": "5435345",
"line1": "1 SS ",
"city": "San Jose",
"state": "CA",
"postal_code": "95131",
"country_code": "US"
},
"phone": "4547567",
"country_code": "US"
}
}

Resources