Change entry in json file that matches the condition in CloudFront Update Distribution - bash

We have a scenario where we need to replace the TargetOriginID of CacheBehaviour in the distribution of a json file. We need to replace the Existing TargetOriginID with New Values. I have tried with below jq command but not getting any closer
for targetoriginID in $(jq '(.CacheBehaviors.Items[].TargetOriginId)' distconfig.json);
do
echo "#######fetch the new value from change behaviour json file"#######
NewValue=$(jq -r "map(select(.targetoriginid == ""$targetoriginID""))[].targetorigindr" changebehaviour.json)
echo "#########replace value in dist config json file with new value from change behaviour###########"
jq -r '(.CacheBehaviors.Items[].TargetOriginId | select(. == "$targetoriginID")) = "$NewValue"' distconfig.json > "tmp" && mv "tmp" distconfig.json
{
"CachedMethods": {
"Quantity": 3,
"Items": [
"HEAD",
"GET",
"OPTIONS"
]
}
},
"SmoothStreaming": false,
"Compress": false,
"LambdaFunctionAssociations": {
"Quantity": 0
},
"FunctionAssociations": {
"Quantity": 0
},
"FieldLevelEncryptionId": "",
"ForwardedValues": {
"QueryString": true,
"Cookies": {
"Forward": "none"
},
"Headers": {
"Quantity": 9,
"Items": [
"Authorization",
"Origin",
"access-control-allow-credentials",
"expires",
"access-control-max-age",
"access-control-allow-headers",
"cache-control",
"access-control-allow-methods",
"pragma"
]
},
"QueryStringCacheKeys": {
"Quantity": 1,
"Items": [
"*"
]
}
},
"MinTTL": 0,
"DefaultTTL": 86400,
"MaxTTL": 31536000
},
"CacheBehaviors": {
"Quantity": 2,
"Items": [
{
"PathPattern": "jkl/*",
"TargetOriginId": "nkl/Prod",
"TrustedSigners": {
"Enabled": false,
"Quantity": 0
},
"TrustedKeyGroups": {
"Enabled": false,
"Quantity": 0
},
"ViewerProtocolPolicy": "redirect-to-https",
"AllowedMethods": {
"Quantity": 7,
"Items": [
"HEAD",
"DELETE",
"POST",
"GET",
"OPTIONS",
"PUT",
"PATCH"
],
"CachedMethods": {
"Quantity": 3,
"Items": [
"HEAD",
"GET",
"OPTIONS"
]
}
},
"SmoothStreaming": false,
"Compress": false,
"LambdaFunctionAssociations": {
"Quantity": 0
},
"FunctionAssociations": {
"Quantity": 0
},
"FieldLevelEncryptionId": "",
"ForwardedValues": {
"QueryString": true,
"Cookies": {
"Forward": "all"
},
"Headers": {
"Quantity": 9,
"Items": [
"Authorization",
"Origin",
"access-control-allow-credentials",
"access-control-max-age",
"access-control-allow-headers",
"cache-control",
"access-control-allow-methods",
"expirers",
"pragma"
]
},
"QueryStringCacheKeys": {
"Quantity": 1,
"Items": [
"*"
]
}
},
"MinTTL": 0,
"DefaultTTL": 86400,
"MaxTTL": 31536000
},
{
"PathPattern": "fgh/*",
"TargetOriginId":"xyz/Prod",
"TrustedSigners": {
"Enabled": false,
"Quantity": 0
},
"TrustedKeyGroups": {
"Enabled": false,
"Quantity": 0
},
"ViewerProtocolPolicy": "redirect-to-https",
"AllowedMethods": {
"Quantity": 7,
"Items": [
"HEAD",
"DELETE",
"POST",
"GET",
"OPTIONS",
"PUT",
"PATCH"
],
"CachedMethods": {
"Quantity": 3,
"Items": [
"HEAD",
"GET",
"OPTIONS"
]
}
},
"SmoothStreaming": false,
"Compress": false,
"LambdaFunctionAssociations": {
"Quantity": 0
},
"FunctionAssociations": {
"Quantity": 0
},
"FieldLevelEncryptionId": "",
"ForwardedValues": {
"QueryString": true,
"Cookies": {
"Forward": "none"
},
"Headers": {
"Quantity": 10,
"Items": [
"access-control-allow-origin",
"authorization",
"Origin",
"access-control-allow-credentials",
"access-control-max-age",
"access-control-allow-headers",
"cache-control",
"access-control-allow-methods",
"expirers",
"pragma"
]
},
"QueryStringCacheKeys": {
"Quantity": 1,
"Items": [
"*"
]
}
},
"MinTTL": 0,
"DefaultTTL": 0,
"MaxTTL": 0
}
]
}
Looking for a solution to make bulk change in CacheBehaviour for all TargetOriginID's

Providing a second answer with some assumption. Let's start with a minimal example input:
{
"CacheBehaviors": {
"Quantity": 2,
"Items": [
{
"PathPattern": "jkl/*",
"TargetOriginId": [
"nkl",
"something else"
]
},
{
"PathPattern": "fgh/*",
"TargetOriginId": [
"xyz",
"abc"
]
}
]
},
"IsIPV6Enabled": true
}
Assuming you want to define a mapping "{old1: new1, old2: new2, old3: new3, …}", the following program could work and is easily extendable:
(.CacheBehaviors.Items[].TargetOriginId[]) |= ({
nkl: "wkl",
xyz: "123",
"something else": "is changed too",
"not found": "never replaced"
}[.] // .)
And if you like to have your mapping at the top/start of your program, bind it to a variable:
{
nkl: "wkl",
xyz: "123",
"something else": "is changed too",
"not found": "rever replaced"
} as $mapping
| (.CacheBehaviors.Items[].TargetOriginId[]) |= ($mapping[.] // .)
Output after running through above's program:
{
"CacheBehaviors": {
"Quantity": 2,
"Items": [
{
"PathPattern": "jkl/*",
"TargetOriginId": [
"wkl",
"is changed too"
]
},
{
"PathPattern": "fgh/*",
"TargetOriginId": [
"123",
"abc"
]
}
]
},
"IsIPV6Enabled": true
}
Is that what you are after? This chooses the new value by key from an object/dictionary/map and falls back to the current value if no mapping was found.
… |= ({ old: "new" }[.] // .)
The mapping itself could be provided as an argument to jq from a separate file:
jq --slurpfile mapping old_to_new_ids.json \
'(.CacheBehaviors.Items[].TargetOriginId[]) |= ($mapping[0][.] // .)'
With the contents of file old_to_new_ids.json simply being:
{
"nkl": "wkl",
"xyz": "123",
"something else": "is changed too",
"not found": "never replaced"
}

As of this writing, the posted JSON is invalid, so it's not clear exactly what is needed, but the following is illustrative:
jq --arg newvalue xyz '
(.. | objects | select(has("TargetOriginId")) | .TargetOriginId) |= $newvalue
'

Fixed (i.e. made valid) and reduced your JSON input down to the necessary minimum (the M in MRE), that is 50 lines.
{
"CacheBehaviors": {
"Quantity": 2,
"Items": [
{
"PathPattern": "jkl/*",
"TargetOriginId": [
"nkl"
],
"TrustedSigners": {
"Enabled": false,
"Quantity": 0
},
"TrustedKeyGroups": {
"Enabled": false,
"Quantity": 0
},
"ViewerProtocolPolicy": "redirect-to-https"
},
{
"PathPattern": "fgh/*",
"TargetOriginId": [
"xyz"
],
"TrustedSigners": {
"Enabled": false,
"Quantity": 0
},
"TrustedKeyGroups": {
"Enabled": false,
"Quantity": 0
},
"ViewerProtocolPolicy": "redirect-to-https"
}
]
},
"CustomErrorResponses": {
"Quantity": 0
},
"Comment": "vvvv",
"Logging": {
"Enabled": true,
"IncludeCookies": false,
"Bucket": "abc.s3.amazonaws.com",
"Prefix": "std"
},
"WebACLId": "",
"HttpVersion": "http2",
"IsIPV6Enabled": true
}
You probably want to run a jq program similar to the following:
(.CacheBehaviors.Items[].TargetOriginId | select(. as $id | "nkl" | IN($id[]))) = ["wkl"]
It selects all TargetOriginIds which contain the value "nkl" and changes the list to only contain "wkl".
Output:
{
"CacheBehaviors": {
"Quantity": 2,
"Items": [
{
"PathPattern": "jkl/*",
"TargetOriginId": [
"wkl"
],
"TrustedSigners": {
"Enabled": false,
"Quantity": 0
},
"TrustedKeyGroups": {
"Enabled": false,
"Quantity": 0
},
"ViewerProtocolPolicy": "redirect-to-https"
},
{
"PathPattern": "fgh/*",
"TargetOriginId": [
"xyz"
],
"TrustedSigners": {
"Enabled": false,
"Quantity": 0
},
"TrustedKeyGroups": {
"Enabled": false,
"Quantity": 0
},
"ViewerProtocolPolicy": "redirect-to-https"
}
]
},
"CustomErrorResponses": {
"Quantity": 0
},
"Comment": "vvvv",
"Logging": {
"Enabled": true,
"IncludeCookies": false,
"Bucket": "abc.s3.amazonaws.com",
"Prefix": "std"
},
"WebACLId": "",
"HttpVersion": "http2",
"IsIPV6Enabled": true
}
From the question it is unclear if "TargetOriginId": ["nkl", "xyz"] should become ["wkl"] or ["wkl", "xyz"]. Or whether ["nkl", "xyz"] is a match because it contains different values too. Maybe you only want to select those where the full TargetOriginId array matches?
If you only want to match items with single-valued target origin ids, the program becomes a bit simpler:
(.CacheBehaviors.Items[].TargetOriginId | select(. == ["nkl"])) = ["wkl"]
If your target origin ids are always an array and you want to change say "[abc,nkl,xyz]" to "[abc,REPLACED,xyz]", then select only those array elements and assign them the new value.
New input (as I understood it, the Q is quite vague on this):
{
"CacheBehaviors": {
"Quantity": 2,
"Items": [
{
"PathPattern": "jkl/*",
"TargetOriginId": [
"nkl",
"something else"
]
},
{
"PathPattern": "fgh/*",
"TargetOriginId": [
"xyz",
"abc"
]
}
]
},
"IsIPV6Enabled": true
}
jq:
(.CacheBehaviors.Items[].TargetOriginId[] | select(. == "nkl")) = "wkl"
Output:
{
"CacheBehaviors": {
"Quantity": 2,
"Items": [
{
"PathPattern": "jkl/*",
"TargetOriginId": [
"wkl",
"something else"
]
},
{
"PathPattern": "fgh/*",
"TargetOriginId": [
"xyz",
"abc"
]
}
]
},
"IsIPV6Enabled": true
}

Related

jq obtain all values of a field only if inside string array the value V is found

I need to get all Distinct "Node" from an array of data objects only if in the object the "ServiceTags" has the String element "orchestrator".
For example, for the json:
[
{
"Node": "abc",
"ServiceTags": [],
"Type": "",
"Definition": {
"Interval": "0s",
"Timeout": "0s"
},
"CreateIndex": 11241543,
"ModifyIndex": 11241543
},
{
"Node": "xyz",
"ServiceTags": [
"rules",
"rhdm_es",
"rhdm",
"orchestrator"
],
"Type": "http",
"Definition": {
"Interval": "0s",
"Timeout": "0s"
},
"CreateIndex": 12907642,
"ModifyIndex": 12907659
},
{
"Node": "teb",
"ServiceTags": [
"rules",
"orchestrator"
],
"Type": "http",
"Definition": {
"Interval": "0s",
"Timeout": "0s"
},
"CreateIndex": 12907642,
"ModifyIndex": 12907659
},
{
"Node": "iry",
"ServiceTags": [
"rules"
],
"Type": "http",
"Definition": {
"Interval": "0s",
"Timeout": "0s"
},
"CreateIndex": 12907642,
"ModifyIndex": 12907659
}
]
the expected result would be an array that contains the values "xyz" and "teb" because there is a "orchestrator" as part of "ServiceTags" property.
I would appreciate any help, I'm currently making this in a basic shell script that only prints all the ServiceTags:
# Get consul data in json format
consulResult=$(cat -)
# Validate if the json is not null or empty
if [ -z "$consulResult" ];
then
echo "NULL OR EMPTY";
else
echo "Not NULL";
echo $consulResult | jq '.[].ServiceTags'
fi
map(select(.ServiceTags | index("orchestrator")).Node)
Will filter based on if orchestrator exist in the ServiceTags array, then output .Node for those objects
Output:
[
"xyz",
"teb"
]
Try it online!

How to get the total page number for pagination

I try to make my pageable data for weeks everything works but I would like to know the total number of pagination pages here are the data of my mongonDB.
{
"content": [
{
"mame": "iPhone X",
"eategory": "High-Tech",
"productId": "aDPXF7Xq",
"details": {
"vating": "00",
"stocks": "20",
"price": "800000",
"tags": [
{
"tagsl": "Apple",
"tags2": "aull",
"tags3": null
}
],
"brand": "Apple",
"description": "Iphone xX",
"picture": [
{
"picturel": "photol",
"picture2": null,
"picture3": null,
"picture4": null,
"picture5": null
}
],
"thumbnails": [
{
"thumbnaill": "thumbails 1",
"thumbnail2": null,
"thumbnail3": null,
"thumbnail4": null,
"thumbnail5": null
}
]
}
},
{
"mame": "iPhone X",
"eategory": "High-Tech",
"productId": "cjjVqOBk",
"details": {
"rating": "10",
"stocks": "20",
"price": "800000",
"tags": [
{
"tagsl": "Apple",
"tags2": "aull",
"tags3": null
}
],
"brand": "Apple",
"description": "Iphone xX",
"picture": [
{
"picturel": "photol",
"picture2": null,
"picture3": null,
"picture4": null,
"picture5": null
}
],
"thumbnails": [
{
"thumbnaill": "thumbails 1",
"thumbnail2": null,
"thumbnail3": null,
"thumbnail4": null,
"thumbnail5": null
}
]
}
},
{
"mame": "iPhone X",
"eategory": "High-Tech",
"productId": "LhKiRGr6",
"details": {
"vating": "10",
"stocks": "20",
"price": "800000",
"tags": [
{
"tagsl": "Apple",
"tags2": "aull",
"tags3": null
}
],
"brand": "Apple",
"description": "Iphone xX",
"picture": [
{
"picturel": "photol",
"picture2": null,
"picture3": null,
"picture4": null,
"picture5": null
}
],
"thumbnails": [
{
"thumbnaill": "thumbails 1",
"thumbnail2": null,
"thumbnail3": null,
"thumbnail4": null,
"thumbnail5": null
}
]
}
},
{
"mame": "iPhone X",
"eategory": "High-Tech",
"productId": "dgCvi8NJ",
"details": {
"vating": "10",
"stocks": "20",
"price": "800000",
"tags": [
{
"tagsl": "Apple",
"tags2": "aull",
"tags3": null
}
],
"brand": "Apple",
"description": "Iphone xX",
"picture": [
{
"picturel": "photol",
"picture2": null,
"picture3": null,
"picture4": null,
"picture5": null
}
],
"thumbnails": [
{
"thumbnaill": "thumbails 1",
"thumbnail2": null,
"thumbnail3": null,
"thumbnail4": null,
"thumbnail5": null
}
]
}
}
],
"pageable": {
"sort": {
"sorted": false,
"unsorted": true,
"empty": true
},
"offset": 0,
"pageNumber": 0,
"pageSize": 20,
"paged": true,
"unpaged": false
},
"totalPages": 1,
"totalElements": 4,
"last": true,
"size": 20,
"sort": {
"sorted": false,
"unsorted": true,
"empty": true
},
"number": 0,
"numberOfElements": 4,
"first": true,
"empty": false
}
Now here is the data paging code
#GetMapping("/engine/search")
public PageImpl<Products> engineSearch(#RequestParam("p") String query, #RequestParam(value = "page", defaultValue = "0") int page, Pageable pageable) {
Criteria criteria = new Criteria();
final Aggregation aggregation = Aggregation.newAggregation(
Aggregation.match( criteria.orOperator(
Criteria.where( "name" ).regex( query, "i" ),
Criteria.where( "details.brand" ).regex( query, "i" ),
Criteria.where( "details.tags.tags1" ).regex( query, "i" ),
Criteria.where( "details.tags.tags2" ).regex( query, "i" ),
Criteria.where( "details.tags.tags3" ).regex( query, "i" )
)),
Aggregation.skip(page * 4),
Aggregation.limit(4)
);
List<Products> filter = mongoTemplate.aggregate(aggregation, "Product", Products.class).getMappedResults();
return new PageImpl<Products>(filter, pageable, filter.size());
}
Mongodb 3.4 has introduced $facet aggregation which processes multiple aggregation pipelines within a single stage on the same set of input documents.
Using $facet and $group you can find documents with $limit and can get total count.
You can use below aggregation in mongodb 3.6
db.collection.aggregate([
{ "$facet": {
"totalData": [
{ "$match": { }},
{ "$skip": 10 },
{ "$limit": 10 }
],
"totalCount": [
{ "$count": "count" }
]
}}
])

process json using jq in bash script

Below is my json which I need to process using jq in bash script. I need to get the "Id" column value. Since in this json there are 3 records, record with maximum value id would be returned. So after processing of below json I should get 170.
I am newbie and have very limited exposure to bash.
{
"count": 3,
"value": [
{
"properties": {},
"tags": [], "validationResults": [],
"plans": [
{
"planId": "49699e0f-b893-4633-bc05-754b8a562d07"
}
], "triggerInfo": {},
"id": 170,
"buildNumber": "20181011.8", "status": "completed", "result": "succeeded", "queueTime": "2018-10-11T15:56:24.9611153Z", "startTime": "2018-10-11T15:56:28.3668144Z", "finishTime": "2018-10-11T15:57:20.5163422Z",
"url": "https://indiatelecom.visualstudio.com/d354caa2-2e88-414a-829b-25df3aceaaaf/_apis/build/Builds/170",
"buildNumberRevision": 8, "uri": "vstfs:///Build/Build/170",
"sourceBranch": "refs/heads/master", "sourceVersion": "4303c19f8fda79e35fcb598219d5dca6bb274c2d",
"priority": "normal", "reason": "manual", "lastChangedDate": "2018-10-11T15:57:20.797Z", "parameters": "{\"system.debug\":\"false\"}",
"orchestrationPlan": {
"planId": "49699e0f-b893-4633-bc05-754b8a562d07"
}, "keepForever": false, "retainedByRelease": false, "triggeredByBuild": null
},
{ "properties": {}, "tags": [], "validationResults": [],
"plans": [ { "planId": "15026a2f-c725-4e52-974b-61e01a940661"
} ],
"triggerInfo": {},
"id": 160,
"buildNumber": "20181009.20", "status": "completed", "result": "succeeded", "queueTime": "2018-10-09T16:47:42.2954075Z", "startTime": "2018-10-09T16:47:43.8034575Z",
"finishTime": "2018-10-09T16:48:35.8340469Z", "url": "https://indiatelecom.visualstudio.com/d354caa2-2e88-414a-829b-25df3aceaaaf/_apis/build/Builds/160",
"buildNumberRevision": 20, "uri": "vstfs:///Build/Build/160",
"sourceBranch": "refs/heads/master", "sourceVersion": "19a55c7482083785265b86015150521b40230c11",
"priority": "normal", "reason": "manual",
"lastChangedDate": "2018-10-09T16:48:36.057Z", "parameters": "{\"system.debug\":\"false\"}",
"orchestrationPlan": {
"planId": "15026a2f-c725-4e52-974b-61e01a940661"
},
"keepForever": false, "retainedByRelease": false,
"triggeredByBuild": null },
{
"properties": {}, "tags": [],
"validationResults": [], "plans": [
{
"planId": "e45d9da8-4d95-42b7-aa23-478e1c1c49f5"
}
],
"triggerInfo": {},
"id": 147,
"buildNumber": "20181009.7", "status": "completed",
"result": "succeeded", "queueTime": "2018-10-09T15:15:47.0248009Z",
"startTime": "2018-10-09T15:15:50.8899892Z", "finishTime": "2018-10-09T15:16:47.7866356Z",
"url": "https://indiatelecom.visualstudio.com/d354caa2-2e88-414a-829b-25df3aceaaaf/_apis/build/Builds/147",
"buildNumberRevision": 7, "uri": "vstfs:///Build/Build/147",
"sourceBranch": "refs/heads/master", "sourceVersion": "70fccb138a2f2a9dfe18290c468959102f504067",
"priority": "normal", "reason": "manual",
"lastChangedDate": "2018-10-09T15:16:48.16Z",
"parameters": "{\"system.debug\":\"false\"}", "orchestrationPlan": {
"planId": "e45d9da8-4d95-42b7-aa23-478e1c1c49f5"
}, "keepForever": false, "retainedByRelease": false,
"triggeredByBuild": null }
] }
The id's are stored in an array under the key value. .value[].id lists the ids, if you put them into an array, you can call max on it:
jq '[.value[].id] | max' < file.json

How to customize the Label Frequency when zoom in [amChart(serial)]

I am using amChart(serial) for showing graph. I have data of length more than 4000. I managed to show the last label value of category axis through "labelFunction" : formatLabel,. But now the problem is to set the label frequency =10. whenever a user zoom in the graph.
Please help, Thanks in advance.
here is code,
var chart= AmCharts.makeChart("rmChart",
{
"theme": "light",
"autoMargins": true, "chartScrollbar": {},
"type": "serial",
"categoryField": 'x-distance',
"legend": {
"useGraphSettings": false
},
"zoomControl": {
"panControlEnabled": false,
"zoomControlEnabled": false
},
"categoryAxis": {
"autoGridCount":false,
"includeHidden":true,
"dashLength":1,
" axisAlpha":1,
"gridPosition": "start",
"labelFunction" : formatLabel,
"minHorizontalGap":20,
"title": "Distance In Meters [m]"
},
"chartCursor": {
"zoomable":true,
"enabled": true,
},
"chartScrollbar": {
"enabled": false
},
"trendLines": [],
"valueAxes": [{
}],
"graphs": [
{
"balloonText": "Distance:[[x-axis]] dB:[[y-axis-0]]",
"fillAlphas": 0,
"fillToAxis": "x",
"lineAlpha": 1,
"valueField": "x-axis",
"valueField": "y-axis-0",
"lineColor": "#FF6600",
},
],
"guides": [],
"valueAxes": [
{
"id": "ValueAxis-1",
"title": "Strength [dB]",
"titleFontSize": 15,
}
],
"balloon": {},
"legend": {
"enabled": false,
"useGraphSettings": false
},
"titles": [
{
"id": "Title-1",
"size": 15,
"text": data[0]["date"]
}
],
"dataProvider": data,
export: {
enabled: false
},
"export": {
"enabled": true,
"menu": []
},
"chartScrollbar": {
"enabled":false
},
});
function formatLabel(value, valueText, axis) {
if (valueText.category%2==0) {
var lastNumber = (Math.abs(valueText.category) % 10)
if (lastNumber==0) {
return valueText.category;
}
}
if (valueText.category==data.length-1) {
return valueText.category;
}
else{
return "";
}
}
here is link to the jsfiddle
https://jsfiddle.net/8zw9h007/10/

Combine JSON objects in Ruby, and output to CSV

Let's say I have two JSON objects (call them 'websites' and 'links'). I need to end up with both objects in a single CSV (ideally in separate columns).
I'm working with something like this:
File.open("file.json", "w") do |f|
combined = [websites, links]
f.write(JSON.pretty_generate(combined))
end
And then I'm using the Ruby gem json2csv to convert this file to a CSV. But when I do, I get the following error:
error: undefined method 'keys' for #<Array:0x007fea8a8e33f8>
I can't figure out what's wrong. When I look in file.json, it appears to be structured like this: [{websites}, {links}]. From my limited knowledge of JSON, I think that's right, but I could easily be wrong.
Also, I know this won't get me separate columns in the CSV. If anyone has an answer for that part, major bonus points.
EDIT: JSON examples included below; error message changed after minor fix on my end.
websites:
{
"uri": "https://v1/websites",
"id": 28235674,
"background": null,
"createdDate": 1399585684000,
"lastActivityDate": 1430682494000,
"lastCommunicationDate": 1430682494000,
"lastNonCommunicationChronicleDate": 1430330886000,
"lastModifiedDate": 1449263116000,
"lastViewedDate": 1421429034000,
"preferredContactType": null,
"rss": "",
"emailAddresses": [
{
"email": "",
"type": "Work"
},
{
"email": "",
"type": "Work"
},
{
"email": "",
"type": "Work"
},
{
"email": "not found",
"type": "Work"
}
],
"phoneNumbers": [
],
"streetAddresses": [
],
"socialNetworks": [
{
"profileUrl": "http://twitter.com",
"name": "Twitter"
},
{
"profileUrl": "http://www.facebook.com",
"name": "Facebook"
},
{
"profileUrl": "http://plus.google.com",
"name": "GooglePlus"
},
{
"profileUrl": "http://www.linkedin.com",
"name": "LinkedIn"
},
{
"profileUrl": "http://twitter.com",
"name": "Twitter"
}
],
"contactUrls": [
],
"tags": [
"tag1",
"tag2"
],
"mostRecentActivity": "https://v1/history",
"mostRecentChronicle": "https://v1/history",
"mostRecentCommunication": "https://v1/history",
"mostRecentNonCommunicationChronicle": "https://v1/history",
"projectStates": "https://v1/websites",
"history": "https://v1/history",
"customFieldValues": [
],
"name": "",
"primaryDomain": "",
"domains": [
""
],
"associatedPeople": "https://v1/people",
"payments": "https://payments",
"links": "https://v1/links",
"type": "https://v1/websites"
}
links:
{
"uri": "https://v1/links/custom_fields",
"id": 15529329,
"value": "Name",
"backgroundColor": null,
"customField": "https://v1/links/custom_fields"
}
combined output:
[
{
"uri": "https://v1/websites",
"id": 28235674,
"background": null,
"createdDate": 1399585684000,
"lastActivityDate": 1430682494000,
"lastCommunicationDate": 1430682494000,
"lastNonCommunicationChronicleDate": 1430330886000,
"lastModifiedDate": 1449263116000,
"lastViewedDate": 1421429034000,
"preferredContactType": null,
"rss": "",
"emailAddresses": [
{
"email": "",
"type": "Work"
},
{
"email": "",
"type": "Work"
},
{
"email": "",
"type": "Work"
},
{
"email": "not found",
"type": "Work"
}
],
"phoneNumbers": [
],
"streetAddresses": [
],
"socialNetworks": [
{
"profileUrl": "http://twitter.com/",
"name": "Twitter"
},
{
"profileUrl": "http://www.facebook.com",
"name": "Facebook"
},
{
"profileUrl": "http://plus.google.com",
"name": "GooglePlus"
},
{
"profileUrl": "http://www.linkedin.com/",
"name": "LinkedIn"
},
{
"profileUrl": "http://twitter.com/",
"name": "Twitter"
}
],
"contactUrls": [
],
"tags": [
"tag1",
"tag2"
],
"mostRecentActivity": "https://v1/history/",
"mostRecentChronicle": "https://v1/history/",
"mostRecentCommunication": "https://v1/history/",
"mostRecentNonCommunicationChronicle": "https://v1/history/",
"projectStates": "https://v1/websites/",
"history": "https://v1/history",
"customFieldValues": [
],
"name": "",
"primaryDomain": "",
"domains": [
""
],
"associatedPeople": "https://v1/people",
"links": "https://v1/links",
"type": "https://v1/websites"
},
{
"uri": "https://links/custom_fields",
"id": 15529329,
"value": "Name",
"backgroundColor": null,
"customField": "https://links/custom_fields"
}
]
JSON.pretty_generate() is expecting a hash and you are passing an array of 2 hashes. Start with: combined.map { |c| f.write(JSON.pretty_generate(c)) } and then mapping them into your CSV should be as easy as following the CSV documentation.

Resources