Adding key value pair in dictionary swift - arrays

Given this
var applicationPayload =
[
"main": [
"key1": "value1",
"key2": [
"inside1": "insideValue1"
]
]
]
How can I append or add something inside key2 property.. I'd like the result to be something like this
[
"main": [
"key1": "value1",
"key2": [
"newInside2": "newInsideValue2",
"inside1": "insideValue1"
]
]
]
I tried doing this does not seems to work
applicationPayload["main"]["key2"]["newInside2"] = "newInsideValue2"

You could create a variable containing the current value of key2 and modify that before rewriting it back into the original dictionary:
import Foundation
var applicationPayload =
[
"main": [
"key1": "value1",
"key2": [
"inside1": "insideValue1"
]
]
]
print("Before: \(applicationPayload)")
var key2Value = applicationPayload["main"]?["key2"] as! [String: String]
key2Value["newInside2"] = "newInsideValue2"
applicationPayload["main"]?["key2"] = key2Value
print("After: \(applicationPayload)")
Output:
Before: ["main": ["key1": "value1", "key2": ["inside1": "insideValue1"]]]
After: ["main": ["key1": "value1", "key2": ["inside1": "insideValue1", "newInside2": "newInsideValue2"]]]

You can try for easy access/assign with keyPath
// get value of key2
var res = applicationPayload[keyPath:"main.key2"] as! [String:Any]
// add the other key
res["newInside2"] = "newInsideValue2"
// mutate key2
applicationPayload[keyPath:"main.key2"] = res
For more about keypaths check https://oleb.net/blog/2017/01/dictionary-key-paths/

Related

Parse JSON Elements in Array with jq

I'm trying to figure out how to print just the attributes->preferences section in the following JSON example using jq. What's throwing me is that this is an array that contains multiple entries that are identified by an id key value pair and I need the attributes->preferences section just from the array element with id 1 without knowing the exact order of the array entries ahead of time.
[
{
"id": 1,
"attributes": {
"preferences": {
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": "value4",
...
}
},
...
},
{
"id": 2,
...
},
{
"id": 4,
...
},
...
]
My desired output would be:
{
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": "value4",
...
}
jq '.[] | select( .id == 1 ) | .attributes.preferences'
Demo on jqplay

Parsing json array into an array of maps in react using Lodash

Let's say this is how my json looks like and it contains an array:
{
"status": {
"time": 175
},
"hits": [
{
"key1": "value1",
"key2": "value2"
},
{
"key1": "value1",
"key2": "value2"
},
{
"key1": "value1",
"key2": "value2"
},
]
}
I want to create an array of maps like this in react:
[
{mapkey1: value1, mapkey2: value2},
{mapkey1: value1, mapkey2: value2},
{mapkey1: value1, mapkey2: value2},
]
I already have a map of mapkeys to their xpaths in source json:
{
mapkey1: xpath1,
mapkey2: xpath2
}
How can I parse this map into another array using Lodash?
I know how to do it when there is no array:
_.mapValues(
xpathsMap,
paths => _.get(data, paths),
);
var arr = [
{
"key1": "value1",
"key2": "value2"
},
{
"key1": "value1",
"key2": "value2"
},
{
"key1": "value1",
"key2": "value2"
},
];
var output = [];
_.forEach(arr, function(obj) {
var keys = Object.keys(obj);
var ob = {};
_.forEach(keys, function(key) {
ob['map' + key] = obj[key];
})
output.push(ob);
});
console.log(output);
This will give you the desired output -
[
{mapkey1: "value1", mapkey2: "value2"},
{mapkey1: "value1", mapkey2: "value2"},
{mapkey1: "value1", mapkey2: "value2"}
]
Use Array#map to create a new array of objects. Create the object keys you require, using lodash's _.mapKeys():
const data = {"status":{"time":175},"hits":[{"key1":"value1","key2":"value2"},{"key1":"value1","key2":"value2"},{"key1":"value1","key2":"value2"}]}
const result = data.hits.map((o) => _.mapKeys(o, (val, key) => `map${key}`));
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

Loop through Dictionary in Swift

I have a dictionary which I want to append some items.
**This is my dictionary which I want to achieve: **
let parameters: [String: Any] = [
{
"ResultsList": [{
"UserId": "b806e283-066f-4081-aafe-1fe216a57c35",
"FriendUserId": "7a2ec150-cdb3-4600-84f8-2dab970bfa0c",
"TransferDate": "2017-11-23",
"UserAnswers": [{
"AnswerId": "b7562603-614d-11e7-a7e0-484d7ee0cd26",
"LastAnsweredDate": "2017-11-23",
"QuestionId": "0b60f35e-5d80-11e7-a7e0-484d7ee0cd26"
},
{
"AnswerId": "b7562603-614d-11e7-a7e0-484d7ee0cd26",
"LastAnsweredDate": "2017-11-23",
"QuestionId": "0b60f35e-5d80-11e7-a7e0-484d7ee0cd26"
}
]
}]
}
]
And this is my current dictionary which I want to make the loop and add the items.
let parameters: [String: Any] = [
{
ResultsList: [
{
UserId: “b806e283-066f-4081-aafe-1fe216a57c35”
FriendUserId: “7a2ec150-cdb3-4600-84f8-2dab970bfa0c”
TransferDate: “2017-11-23”
UserAnswers: [
{
AnswerId: “b7562603-614d-11e7-a7e0-484d7ee0cd26"
LastAnsweredDate: “2017-11-23”
QuestionId : “0b60f35e-5d80-11e7-a7e0-484d7ee0cd26"
}
]
}
]
}
]
I have 3 arrays which I want to loop through and append to the dictionary
var AnswerId = [ “b7562603-614d-11e7-a7e0-484d7ee0cd26", “aasdaas-614d-11e7-a7e0-484d7ee0cd26", “b756asd03-614d-11e7-a7e0-484d7ee0cd26"]
var LastAnsweredDate = [“2017-11-23”, “2017-11-23”, “2017-11-22”]
var QuestionId = [“0b60f35e-5d80-11e7-a7e0-484d7ee0cd26",“asdasd-5d80-11e7-a7e0-484d7ee0cd26",“asdasd-5d80-11e7-a7e0-484d7ee0cd26"]
Can someone please help to achieve this result?

I want to create ,multidimensional array in swift with multiple inner arrays

I want to create ,multidimensional array in swift with multiple inner arrays. I want the structure look like this but getting error while creating this. Please guide me how can I create this structure.
class ViewController: UIViewController
{
var data2:[String] = []
var LocationPickerData = ["Mozzarella","Gorgonzola","Provolone","Brie","Maytag Blue"]
var LocationPickerData2 = ["Sharp Cheddar","Monterrey Jack","Stilton","Gouda","Goat Cheese", "Asiago"]
var LocationPickerData3 = ["Goat Cheese", "Asiago"]
var Title = ["twwwitle1","sdfsaf","Prsdfasovosdfasflone","sfsa","sdfsf Blue"]
var Title2 = ["seeefdasf Cheddar","sdfsf Jack","fsaf","Gouda","fasf Cheese", "sdfsf"]
var Title3 = ["ddddsdfs ff", "fasfsdffasf"]
var Name = ["fsdfa","fasfsafsf","Provolone","Brie","Maytag Blue"]
var Name2 = ["afsadff Cheddar","Monterrey Jack","Stilton","Gouda","Goat Cheese", "Asiago"]
var Name3 = ["fffffff ffff", "jjjjjjj"]
var data2 = [
location[
location1[ LocationPickerData ],
location2[ LocationPickerData2],
location3[ LocationPickerData3]
],
titles[
title1[Title],
title2[Title2],
title3[Title3]
],
names[
name1[Name],
name2[Name2],
name3[Name3]
]
]
}
Declare your multidimensional array like this:
var data2 = [
[
LocationPickerData
LocationPickerData2,
LocationPickerData3,
],
[
Title
Title2,
Title3,
],
[
Name
Name2,
Name3,
]
]
Or you can declare a dictionary:
var data2 = [
"Locations": [
LocationPickerData
LocationPickerData2,
LocationPickerData3,
],
"Titles": [
Title
Title2,
Title3,
],
"Names": [
Name
Name2,
Name3,
]
]

JSON how to get squared brackets?

I have the following code:
JSONArray array = new JSONArray();
array.put(allWaitClasses.get(0).allPairs.get(0).pair);
array.put(allWaitClasses.get(0).allPairs.get(1).pair);
array.put(allWaitClasses.get(0).allPairs.get(2).pair);
array.put(allWaitClasses.get(0).allPairs.get(3).pair);
array.put(allWaitClasses.get(0).allPairs.get(4).pair);
array.put(allWaitClasses.get(0).allPairs.get(5).pair);
json = array.toString();
What I get is the following:
[
{
"name": "User I/O"
},
{
"key": "61410583140000"
},
...
]
But what I want is squared brackets:
var data =[
[
1229904000000,
12.74
],
[
1229990400000,
115.20
],
...
Actually I want to follow the template of this API:
https://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=?
So how can I get the squared brackets insteand of the curved ones?
you should iterate over data and push items in following way :
var arrayX = [];
$.forEach( data, function ( item ) {
arrayX .push( [
item.timestamp, item.value
] );
} )

Resources