Merge SwiftyJson Multiple Arrays - json

I am using SwiftyJSON in Swift and I have three arrays that I would like to merge together into one array. If the name already exist, i'll like it to add the value to just get one element.
These are the arrays that I have:
var array1 = JSON(
"name" : "apple",
"Value" : 2
]
var array2 = JSON(
"name" : "apple",
"Value" : 4
]
var array3 = JSON(
"name" : "orange",
"Value" : 10
]
What is the best way to achieve the result below when I print the array?
{
{
"name": "apple"
"value": 6
} {
"name": "orange"
"value": 10
}
}

Related

Parsing json in terraform locals

I have another question about JSON parsing. Imagine I have this JSON:
{
"all_dogs" :[
{
"name": "foo",
"groups": ["morning", "evening"]
},
{
"name": "bar",
"groups": ["evening", "saturday"]
},
{
"name": "feet",
"groups": ["afternoon"]
}
]
}
I can extract all the groups like this:
locals {
all_dogs = jsondecode(file("${path.module}/dogs.json"))
all_groups = toset(flatten(local.all_dogs.all_dogs[*].groups))
}
Now, I’m trying to create a MAP. Each group is a key of the map and values of the maps are the different dogs in that group.
So I would like to create a map like this:
afternoon = [feet]
evening= [foo, bar]
morning= [foo]
saturday= [bar]
I'm trying with something like this and I tried several options... But I can't make it work.
output "ex" {
value = flatten([
for group in local.all_groups: [
for dog in local.all_dogs : {
group = group
dog = dog
}
]
]
)
}
Later on, I would like to use that map to provision some resources. Is this eventually possible?
locals {
all_dogs = jsondecode(file("${path.module}/dogs.json"))
groups = flatten([for d in local.all_dogs.all_dogs : [for g in d.groups : { key : g, value : d.name }]])
}
The value for local groups will be a list of tuples and it will look something like this:
groups = [
{
"key" = "morning"
"value" = "foo"
},
{
"key" = "evening"
"value" = "foo"
},
{
"key" = "evening"
"value" = "bar"
},
{
"key" = "saturday"
"value" = "bar"
},
{
"key" = "afternoon"
"value" = "feet"
},
]
Now we have to create a map from this list:
output "my_map" {
value = {
for g in local.groups : g.key => g.value...
}
}
This will produce the following output:
my_map= {
"afternoon" = [
"feet",
]
"evening" = [
"foo",
"bar",
]
"morning" = [
"foo",
]
"saturday" = [
"bar",
]

How to flatten this json in datafactory

I have a API json response. The response has the same blocks type of data nested and i need to flatten this via the Azure datafactory. The depth of the children is variable. I'm not a expert in ADF and i couldn't find a example of how to fix this. I suspect that i need some recursive function to do this.
Some guidance would be very much appreciated.
Example json:
[
{
"id" : 1,
"name" : "item 1",
"children" : []
},
{
"id" : 2,
"name" : "item 2",
"children" : [
{
"id" : 3,
"name" : "item 3",
"children" : [
{
"id" : 4,
"name" : "item 4",
"children" : []
}
]
}
]
}
]
And i need to transform it into a sql table:
id
name
1
item 1
2
item 2
3
item 3
4
item 4
You will have to use Mapping data flow in Azure Data factory and use multiple Flatten transformations to get the desired output.

Using jq to extract multiple fields and create a new object

I have this particular json object,
[
{
"userid" : "fe2e48b7-858b-4a0d-964a-efb8483a00c4",
"lastupdateddate" : "84798000-13cd-11ea-8080-808080808080",
"transactionid" : "10383117.2216238756",
"accountid" : "10383117.10921962",
"misctransactiondata" : null,
"rawtransactiondata" : "{\"id\":\"1234567\",\"account_id\":\"456451962\"}",
"source" : "gateway",
"transactiondatajson" : "{\"version\":\"v1\",\"transactionId\":\"4234234.2216238756\",\"accountId\":\"345345345.10921962\"}",
"version" : "v1"
}
]
which I'd like to transform into,
{
"transactions": [
{
"version": "v1",
"transactionId": "4234234.2216238756",
"accountId": "345345345.10921962",
"rawData": {
"id": "1234567",
"account_id": "456451962"
}
}
]
}
Currently I have,
jq '{transactions: [.[0] | (.transactiondatajson|fromjson) ]}'
which creates the transactions array of objects however I'm not entirely sure how to create the rawData nested object from .rawtransactiondata
How to best append the object with jq ?
One of many possibilities:
.[]
| {transactions:
[(.transactiondatajson|fromjson)
+ {rawData: (.rawtransactiondata|fromjson)} ] }

Splitting Json to multiple jsons in NIFI

I have the below json file which I want to split in NIFI
Input:
[ {
"id" : 123,
"ticket_id" : 345,
"events" : [ {
"id" : 3322,
"type" : "xyz"
}, {
"id" : 6675,
"type" : "abc",
"value" : "sample value",
"field_name" : "subject"
}, {
"id" : 9988,
"type" : "abc",
"value" : [ "text_file", "json_file" ],
"field_name" : "tags"
}]
}]
and my output should be 3 different jsons like below:
{
"id" : 123,
"ticket_id" : 345,
"events.id" :3322,
"events.type":xyz
}
{
"id" : 123,
"ticket_id" : 345,
"events.id" :6675,
"events.type":"abc",
"events.value": "sample value"
"events.field_name":"subject"
}
{
"id" : 123,
"ticket_id" : 345,
"events.id" :9988,
"events.type":"abc",
"events.value": "[ "text_file", "json_file" ]"
"events.field_name":"tags"
}
I want to know can we do it using splitjson? I mean can splitjson split the json based on the array of json objects present inside the json?
Please let me know if there is a way to achieve this.
If you want 3 different flow files, each containing one JSON object from the array, you should be able to do it with SplitJson using a JSONPath of $ and/or $.*
Using reduce function:
function split(json) {
return json.reduce((acc, item) => {
const events = item.events.map((evt) => {
const obj = {id: item.id, ticket_id: item.ticket_id};
for (const k in evt) {
obj[`events.${k}`] = evt[k];
}
return obj;
});
return [...acc, ...events];
}, []);
}
const input = [{"id":123,"ticket_id":345,"events":[{"id":3322,"type":"xyz"},{"id":6675,"type":"abc","value":"sample value","field_name":"subject"},{"id":9988,"type":"abc","value":["text_file","json_file"],"field_name":"tags"}]}];
const res = split(input);
console.log(res);

Simplifying JSON structure

I have the following JSON structure but am wondering if there would be any way to simplify it further. Can 'ingredient' and 'quantity' be removed from all the entries somehow to help reduce it?
var cooking = {
"recipes" : [
{
"name":"pizza",
"ingredients" : [
{
"ingredient" : "cheese",
"quantity" : "100g"
},
{
"ingredient" : "tomato",
"quantity" : "200g"
}
]
},
{
"name":"pizza 2",
"ingredients" : [
{
"ingredient" : "ham",
"quantity" : "300g"
},
{
"ingredient" : "pineapple",
"quantity" : "300g"
}
]
}
]
};
Yes, you can simplify that quite a bit:
var recipes = {
"pizza": {
"cheese": "100g",
"tomato": "200g"
},
"pizza 2": {
"ham": "300g",
"pineapple": "300g"
}
}
An explanation:
The top level of your example is a single-item object: {"recipes": <...>}. Unless this is a simplified version of an object that will actually have other items in it, that's redundant. Your code knows what it's sending/recieving, so there's no extra information there.
The value of your {"recipes": <...>} object is an array of two-item objects, with the keys "name" and "ingredients". Whenever you have an array like this, it makes more sense (and is more compact) to replace it with an object. As a rule of thumb:
If the keys in an array of objects can be replaced by "key" and "value" and still make sense, replace the array with a single {"key_name": <value>, ...} object.
The same rule applies to your [{"ingredient": <...>, "quantity": <...>}, ...] array: each object can be replaced by a key-value pair and continue to make sense.
The end result is that this representation of the information is 87 characters in length (with extraneous whitespace removed), compared to your original's 249 characters - a 65% reduction.
Definitely. One way would be :
var cooking = {
"recipes" : [
{
"name":"pizza",
"ingredients" : [
"cheese",
"tomato"
],
"quantities" : [ // Have to be in order of ingredients
"100g",
"200g"
]
}
]
}
Or
var cooking = {
"recipes" : [
{
"name":"pizza",
"ingredients" : [ // Putting ingredient and quantity together
"cheese:100g",
"tomato:200g"
]
}
]
}
Since they are all pizzas you can remove the name.
var cooking = {
"recipes" : [
{
"ingredients" : [
"cheese:100g",
"tomato:200g"
]
},
{
"ingredients" : [
"ham:100g",
"pineapple:200g"
]
}
]
}
Hope this simplifies it for you ! Json must be written in a way so that its minimal and comprehensible for both computers and humans.
var cooking = {
"recipes" :
[
{
"name":"pizza",
"cheese": "100g"
"tomato": "200g"
}
,
{
"name":"pizza 2",
"ham": "300g"
"pineapple": "300g"
}
]
}
};