I have a Visual Studio (C#) deployment package (.zip) that I have pushed up to my S3 storage.
I want to run my CloudFormation script and have it create an instance of an IIS server (I have the script for this) and then deploy the Visual Studio web site to it from the S3 storage.
I'm looking for an example of the temple json that would do that
I have a template that does something similar to what you are looking for. Below is a template that I use. It may be more than you need, because it has an auto scaling group, but it will get you started. Basically, you need the IAM user to interact with cloud formation. The script in the UserData starts cf-init, which does the stuff in the metadata section.
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Autoscaling for .net Web application.",
"Parameters": {
"InstanceType": {
"Description": "WebServer EC2 instance type",
"Type": "String",
"Default": "m1.small",
"AllowedValues": [
"t1.micro",
"m1.small",
"m1.medium",
"m1.large",
"m1.xlarge",
"m2.xlarge",
"m2.2xlarge",
"m2.4xlarge",
"c1.medium",
"c1.xlarge",
"cc1.4xlarge",
"cc2.8xlarge",
"cg1.4xlarge"
],
"ConstraintDescription": "Must be a valid EC2 instance type."
},
"IamInstanceProfile": {
"Description": "Name of IAM Profile that will be used by instances to access AWS Services",
"Type": "String",
"Default": "YourProfileName"
},
"KeyName": {
"Description": "The EC2 Key Pair to allow access to the instances",
"Default": "yourkeypair",
"Type": "String"
},
"SpotPriceBid": {
"Description": "Max bid price of spot instances",
"Type": "String",
"Default": ".06"
},
"DeployS3Bucket": {
"Description": "The S3 Bucket where deploy files are stored",
"Type": "String",
"Default": "ApplicationBucket"
},
"DeployWebS3Key": {
"Description": "The zip file that holds the website",
"Type": "String",
"Default": "Application.zip"
},
"DNSHostedZone": {
"Type": "String",
"Default": "example.com.",
"AllowedPattern": "^[\\w\\.]*\\.$",
"ConstraintDescription": "DNSDomain must end with '.'"
},
"DNSSubDomain": {
"Type": "String",
"Default": "yoursubdomain"
}
},
"Mappings": {
"RegionToAMIMap": {
"us-east-1": {
"AMI": "ami-1234567"
}
}
},
"Resources": {
"IAMUser": {
"Type": "AWS::IAM::User",
"Properties": {
"Path": "/",
"Policies": [{
"PolicyName": "webuser",
"PolicyDocument": {
"Statement": [{
"Sid": "Stmt1353842250430",
"Action": [
"s3:GetObject"
],
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::HelgaDogWeb*/*"
]
}, {
"Sid": "Stmt1353842327065",
"Action": [
"cloudformation:DescribeStackResource"
],
"Effect": "Allow",
"Resource": [
"*"
]
}
]
}
}
]
}
},
"IAMUserAccessKey": {
"Type": "AWS::IAM::AccessKey",
"Properties": {
"UserName": {
"Ref": "IAMUser"
}
}
},
"WebSecurityGroup": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"GroupDescription": "Enable Access From Elastic Load Balancer.",
"SecurityGroupIngress": [{
"IpProtocol": "tcp",
"FromPort": "443",
"ToPort": "443",
"SourceSecurityGroupOwnerId": {
"Fn::GetAtt": [
"WebLoadBalancer",
"SourceSecurityGroup.OwnerAlias"
]
},
"SourceSecurityGroupName": {
"Fn::GetAtt": [
"WebLoadBalancer",
"SourceSecurityGroup.GroupName"
]
}
}, {
"IpProtocol": "tcp",
"FromPort": "80",
"ToPort": "80",
"SourceSecurityGroupOwnerId": {
"Fn::GetAtt": [
"WebLoadBalancer",
"SourceSecurityGroup.OwnerAlias"
]
},
"SourceSecurityGroupName": {
"Fn::GetAtt": [
"WebLoadBalancer",
"SourceSecurityGroup.GroupName"
]
}
}
]
}
},
"WebLoadBalancer": {
"Type": "AWS::ElasticLoadBalancing::LoadBalancer",
"Properties": {
"Listeners": [{
"InstancePort": "443",
"InstanceProtocol": "HTTPS",
"LoadBalancerPort": "443",
"Protocol": "HTTPS",
"SSLCertificateId": "arn:aws:iam::123456789101:server-certificate/example"
}
],
"AvailabilityZones": {
"Fn::GetAZs": ""
},
"HealthCheck": {
"HealthyThreshold": "3",
"Interval": "30",
"Target": "HTTP:80/healthcheck.aspx",
"Timeout": 8,
"UnhealthyThreshold": "2"
}
}
},
"WebAsSpotLaunchConfiguration": {
"Type": "AWS::AutoScaling::LaunchConfiguration",
"Metadata": {
"AWS::CloudFormation::Init": {
"config": {
"sources": {
"C:\\inetpub\\wwwroot": {
"Fn::Join": [
"/",
[
"http://s3.amazonaws.com", {
"Ref": "DeployS3Bucket"
}, {
"Ref": "DeployWebS3Key"
}
]
]
}
},
"commands": {
"1-set-appPool-identity": {
"command": "C:\\Windows\\System32\\inetsrv\\appcmd set config /section:applicationPools /[name='DefaultAppPool'].processModel.identityType:LocalSystem",
"waitAfterCompletion": "0"
},
"2-add-http-binding": {
"command": "C:\\Windows\\System32\\inetsrv\\appcmd set site /site.name:\"Default Web Site\" /+bindings.[protocol='http',bindingInformation='*:80:']",
"waitAfterCompletion": "0"
}
}
}
},
"AWS::CloudFormation::Authentication": {
"S3AccessCreds": {
"type": "S3",
"accessKeyId": {
"Ref": "IAMUserAccessKey"
},
"secretKey": {
"Fn::GetAtt": [
"IAMUserAccessKey",
"SecretAccessKey"
]
},
"buckets": [{
"Ref": "DeployS3Bucket"
}
]
}
}
},
"Properties": {
"KeyName": {
"Ref": "KeyName"
},
"ImageId": {
"Fn::FindInMap": [
"RegionToAMIMap", {
"Ref": "AWS::Region"
},
"AMI"
]
},
"IamInstanceProfile": {
"Ref": "IamInstanceProfile"
},
"SecurityGroups": [{
"Ref": "WebSecurityGroup"
}
],
"InstanceType": {
"Ref": "InstanceType"
},
"SpotPrice": {
"Ref": "SpotPriceBid"
},
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"<script>\n",
"\"C:\\Program Files (x86)\\Amazon\\cfn-bootstrap\\cfn-init.exe\" -v -s ", {
"Ref": "AWS::StackName"
},
" -r WebAsSpotLaunchConfiguration ",
" --access-key ", {
"Ref": "IAMUserAccessKey"
},
" --secret-key ", {
"Fn::GetAtt": [
"IAMUserAccessKey",
"SecretAccessKey"
]
},
"\n",
"</script>"
]
]
}
}
}
},
"WebAsSpotGroup": {
"Type": "AWS::AutoScaling::AutoScalingGroup",
"Properties": {
"AvailabilityZones": {
"Fn::GetAZs": ""
},
"HealthCheckGracePeriod": "120",
"HealthCheckType": "EC2",
"LaunchConfigurationName": {
"Ref": "WebAsSpotLaunchConfiguration"
},
"LoadBalancerNames": [{
"Ref": "WebLoadBalancer"
}
],
"MaxSize": "20",
"MinSize": "1",
"DesiredCapacity": "1"
}
},
"WebAsSpotScaleUpPolicy": {
"Type": "AWS::AutoScaling::ScalingPolicy",
"Properties": {
"AdjustmentType": "PercentChangeInCapacity",
"AutoScalingGroupName": {
"Ref": "WebAsSpotGroup"
},
"Cooldown": "420",
"ScalingAdjustment": "200"
}
},
"WebAsSpotScaleDownPolicy": {
"Type": "AWS::AutoScaling::ScalingPolicy",
"Properties": {
"AdjustmentType": "ChangeInCapacity",
"AutoScalingGroupName": {
"Ref": "WebAsSpotGroup"
},
"Cooldown": "60",
"ScalingAdjustment": "-1"
}
},
"WebAsSpotScaleUpAlarm": {
"Type": "AWS::CloudWatch::Alarm",
"Properties": {
"MetricName": "CPUUtilization",
"Namespace": "AWS/EC2",
"Statistic": "Average",
"Period": "60",
"EvaluationPeriods": "1",
"Threshold": "75",
"AlarmActions": [{
"Ref": "WebAsSpotScaleUpPolicy"
}
],
"Dimensions": [{
"Name": "AutoScalingGroupName",
"Value": {
"Ref": "WebAsSpotGroup"
}
}
],
"ComparisonOperator": "GreaterThanThreshold"
}
},
"WebAsSpotScaleDownAlarm": {
"Type": "AWS::CloudWatch::Alarm",
"Properties": {
"MetricName": "CPUUtilization",
"Namespace": "AWS/EC2",
"Statistic": "Average",
"Period": "60",
"EvaluationPeriods": "2",
"Threshold": "50",
"AlarmActions": [{
"Ref": "WebAsSpotScaleDownPolicy"
}
],
"Dimensions": [{
"Name": "AutoScalingGroupName",
"Value": {
"Ref": "WebAsSpotGroup"
}
}
],
"ComparisonOperator": "LessThanThreshold"
}
},
"DNSRecord": {
"Type": "AWS::Route53::RecordSet",
"Properties": {
"HostedZoneName": {
"Ref": "DNSHostedZone"
},
"Comment": "VPN Host. Created by Cloud Formation.",
"Name": {
"Fn::Join": [
".",
[{
"Ref": "DNSSubDomain"
}, {
"Ref": "DNSHostedZone"
}
]
]
},
"Type": "CNAME",
"TTL": "150",
"ResourceRecords": [{
"Fn::GetAtt": [
"WebLoadBalancer",
"CanonicalHostedZoneName"
]
}
]
},
"DependsOn": "WebLoadBalancer"
}
},
"Outputs": {}
}
I havent tried it myself, but this post, on the AWS site, Using Amazon CloudFront with ASP.NET Apps maybe somewhere to start.
Related
I'm using NJsonSchema to convert a normal Json to Schema.
However, NJsonSchema returns the Schema with $ref fields, but I want to have the actual structure.
For example:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"Property1": {
"$ref": "#/definitions/Property1"
},
"Property2": {
"$ref": "#/definitions/Property2"
},
"Property3": {
"type": "array",
"items": {
"$ref": "#/definitions/Property3"
}
},
"Property4": {
"type": "array",
"items": {
"$ref": "#/definitions/Property4"
}
}
},
"definitions": {
"Property1": {
"type": "object",
"properties": {
"Property1_1": {
"type": "string"
},
"Property1_2": {
"type": "boolean"
},
"Property1_3": {
"type": "integer"
},
"Property1_4": {
"type": "integer"
}
}
},
"Property2": {
"type": "object",
"properties": {
"Property2_1": {
"$ref": "#/definitions/Property2_1"
},
"Property2_2": {
"$ref": "#/definitions/Property2_2"
}
}
},
"Property2_1": {
"type": "object",
"properties": {
"Property2_1_1": {
"type": "array",
"items": {
"$ref": "#/definitions/Property2_1_1"
}
},
"Property2_1_2": {
"type": "array",
"items": {
"$ref": "#/definitions/Property2_1_2"
}
},
"Property2_1_3": {
"type": "array",
"items": {
"$ref": "#/definitions/Property2_1_3"
}
},
"Property2_1_4": {
"type": "array",
"items": {
"$ref": "#/definitions/Property2_1_4"
}
}
}
},
"Property2_1_1": {
"type": "object",
"properties": {
"filename": {
"type": "string"
},
"interface": {
"type": "string"
},
"_Comment": {
"type": "string"
}
}
},
"Property2_1_2": {
"type": "object",
"properties": {
"filename": {
"type": "string"
},
"interface": {
"type": "string"
},
"_Comment": {
"type": "string"
}
}
},
"Property2_1_3": {
"type": "object",
"properties": {
"filename": {
"type": "string"
},
"interface": {
"type": "string"
}
}
},
"Property2_1_4": {
"type": "object",
"properties": {
"filename": {
"type": "string"
},
"interface": {
"type": "string"
}
}
},
"Property2_2": {
"type": "object",
"properties": {
"dtm_file_name": {
"type": "string"
},
"dtm_file_name_ext": {
"type": "string"
},
"_Comment": {
"type": "string"
}
}
},
"Property3": {
"type": "object",
"properties": {
"offset": {
"type": "integer"
},
"value": {
"type": "string"
}
}
},
"Property4": {
"type": "object",
"properties": {
"attr_name": {
"type": "string"
},
"offset": {
"type": "integer"
}
}
}
}
}
Got the above JSON using:
JsonSchema bodySchema = JsonSchema.FromSampleJson(jsonStr);
string schemaStr = bodySchema.ToJson();
How can I remove these references and replace to the actual structures?
Such as:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"Property1": {
"type": "object",
"properties": {
"Property1_1": {
"type": "string"
},
"Property1_2": {
"type": "boolean"
},
"Property1_3": {
"type": "integer"
},
"Property1_4": {
"type": "integer"
}
}
},
"Property2": {
"type": "object",
"properties": {
"Property2_1": {
"type": "object",
"properties": {
"Property2_1_1": {
"type": "array",
"items": {
"type": "object",
"properties": {
"filename": {
"type": "string"
},
"interface": {
"type": "string"
},
"_Comment": {
"type": "string"
}
}
}
},
"Property2_1_2": {
"type": "array",
"items": {
"type": "object",
"properties": {
"filename": {
"type": "string"
},
"interface": {
"type": "string"
},
"_Comment": {
"type": "string"
}
}
}
},
"Property2_1_3": {
"type": "array",
"items": {
"type": "object",
"properties": {
"filename": {
"type": "string"
},
"interface": {
"type": "string"
}
}
}
},
"Property2_1_4": {
"type": "array",
"items": {
"type": "object",
"properties": {
"filename": {
"type": "string"
},
"interface": {
"type": "string"
}
}
}
}
}
},
"Property2_2": {
"type": "object",
"properties": {
"dtm_file_name": {
"type": "string"
},
"dtm_file_name_ext": {
"type": "string"
},
"_Comment": {
"type": "string"
}
}
}
}
},
"Property3": {
"type": "array",
"items": {
"type": "object",
"properties": {
"offset": {
"type": "integer"
},
"value": {
"type": "string"
}
}
}
},
"Property4": {
"type": "array",
"items": {
"type": "object",
"properties": {
"attr_name": {
"type": "string"
},
"offset": {
"type": "integer"
}
}
}
}
}
}
Is there any method that does that inside NJsonSchema or some other way?
Regards,
Thiago
I am trying to deserialize the JSON downloaded from the following site downloaded as RawData
Json from the Site
but following error is being thrown
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type '' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
I tried using following methods
Root[] roots= JsonConvert.DeserializeObject<Root[]>(jsonString);
and
var roots = JsonConvert.DeserializeObject<List<Root>>(jsonString);
I used following class
public class Root
{
public List<object> posts { get; set; }
public List<Person> persons { get; set; }
public List<Organization> organizations { get; set; }
public Meta meta { get; set; }
public List<Membership> memberships { get; set; }
public List<Event> events { get; set; }
public List<Area> areas { get; set; }
}
Following is the example JSON
"posts": [
],
"persons": [
{
"birth_date": "1957-08-09",
"contact_details": [
{
"type": "email",
"value": "hariomsingh.rathore#sansad.nic.in"
}
],
"email": "hariomsingh.rathore#sansad.nic.in",
"gender": "male",
"id": "0094ff14-ff6c-440a-96fc-f0bd68068569",
"identifiers": [
{
"identifier": "4655",
"scheme": "everypolitician_legacy"
},
{
"identifier": "hariomsinghrathore",
"scheme": "prsindia"
},
{
"identifier": "Q16897877",
"scheme": "wikidata"
}
],
"image": "http://164.100.47.132/mpimage/photo/4655.jpg",
"images": [
{
"url": "http://164.100.47.132/mpimage/photo/4655.jpg"
}
],
"links": [
{
"note": "Wikipedia (en)",
"url": "https://en.wikipedia.org/wiki/Hariom_Singh_Rathore"
}
],
"name": "Yavatmal-Washim",
"other_names": [
{
"lang": "en",
"name": "Yavatmal-Washim Lok Sabha constituency",
"note": "multilingual"
},
{
"lang": "hi",
"name": "यवतमाल-वाशिम लोक सà¤à¤¾ निरà¥à¤µà¤¾à¤šà¤¨ कà¥à¤·à¥‡à¤¤à¥à¤°",
<?xml version="1.0" encoding="UTF-8"?> {
"posts": [
],
"persons": [
{
"birth_date": "1957-08-09",
"contact_details": [
{
"type": "email",
"value": "hariomsingh.rathore#sansad.nic.in"
}
],
"email": "hariomsingh.rathore#sansad.nic.in",
"gender": "male",
"id": "0094ff14-ff6c-440a-96fc-f0bd68068569",
"identifiers": [
{
"identifier": "4655",
"scheme": "everypolitician_legacy"
},
{
"identifier": "hariomsinghrathore",
"scheme": "prsindia"
},
{
"identifier": "Q16897877",
"scheme": "wikidata"
}
],
"image": "http://164.100.47.132/mpimage/photo/4655.jpg",
"images": [
{
"url": "http://164.100.47.132/mpimage/photo/4655.jpg"
}
],
"links": [
{
"note": "Wikipedia (en)",
"url": "https://en.wikipedia.org/wiki/Hariom_Singh_Rathore"
}
],
"name": "Rathore, Shri Hariom Singh",
"other_names": [
{
"lang": "en",
"name": "Hariom Singh Rathore",
"note": "multilingual"
},
{
"lang": "gu",
"name": "હરિઓમ સિંહ રાઠૌડ઼",
"note": "multilingual"
}
]
},
{
"birth_date": "1975-09-10",
"contact_details": [
{
"type": "email",
"value": "ravneetbittu#gmail.com"
}
],
"email": "ravneetbittu#gmail.com",
"family_name": "Singh",
"gender": "male",
"id": "01727319-7f2b-465b-825c-1d7a94a54f70",
"identifiers": [
{
"identifier": "4429",
"scheme": "everypolitician_legacy"
},
{
"identifier": "ravneetsingh",
"scheme": "prsindia"
},
],
"image": "http://164.100.47.132/mpimage/photo/4429.jpg",
"images": [
{
"url": "http://164.100.47.132/mpimage/photo/4429.jpg"
}
],
"links": [
{
"note": "Wikipedia (en)",
"url": "https://en.wikipedia.org/wiki/Ravneet_Singh_Bittu"
},
{
"note": "Wikipedia (pa)",
"url": "https://pa.wikipedia.org/wiki/ਰਵਨੀਤ_ਸਿੰਘ"
}
],
"name": "Singh, Shri Ravneet",
"other_names": [
"note": "multilingual"
},
{
"lang": "mr",
"name": "यवतमाळ-वाशिम (लोकसà¤à¤¾ मतदारसंघ)",
"note": "multilingual"
},
{
"lang": "ta",
{
"lang": "en",
"name": "Ravneet Singh",
"note": "multilingual"
},
{
"lang": "te",
"name": "రవనీతౠసింగౠబిటà±à°Ÿà±‚",
"note": "multilingual"
}
]
},
{
"birth_date": "1958-09-07",
"contact_details": [
{
"type": "email",
"value": "bhairon.prasad#sansad.nic.in"
}
],
"email": "bhairon.prasad#sansad.nic.in",
"family_name": "Mishra",
"gender": "male",
"id": "02670d6a-6b60-4e7b-b0cd-b4fc7d6c3bca",
"identifiers": [
{
"identifier": "4626",
"scheme": "everypolitician_legacy"
},
{
"identifier": "Q16902096",
"scheme": "wikidata"
}
],
"image": "http://164.100.47.132/mpimage/photo/4626.jpg",
"images": [
{
"url": "http://164.100.47.132/mpimage/photo/4626.jpg"
}
],
"links": [
{
"note": "Wikipedia (en)",
"url": "https://en.wikipedia.org/wiki/Bhairon_Prasad_Mishra"
},
{
"note": "Wikipedia (hi)",
"url": "https://hi.wikipedia.org/wiki/à¤à¥ˆà¤°à¥‹à¤‚_पà¥à¤°à¤¸à¤¾à¤¦_मिशà¥à¤°"
}
],
"name": "யவதà¯à®®à®¾à®³à¯-வாசிம௠மகà¯à®•à®³à®µà¯ˆà®¤à¯ தொகà¯à®¤à®¿",
"note": "multilingual"
}
],
"type": "constituency"
},
{
"id": "zahirabad",
"identifiers": [
{
"identifier": "Q8064692",
"scheme": "wikidata"
}
],
"name": "Zahirabad",
"other_names": [
{
"lang": "en",
"name": "Zahirabad Lok Sabha constituency",
"note": "multilingual"
},
{
"lang": "hi",
"name": "ज़हीराबाद लोक सà¤à¤¾ निरà¥à¤µà¤¾à¤šà¤¨ कà¥à¤·à¥‡à¤¤à¥à¤° समà¥à¤ªà¤¾à¤¦à¤¨",
"note": "multilingual"
},
{
"lang": "mr",
"name": "à¤à¤¹à¥€à¤°à¤¾à¤¬à¤¾à¤¦ (लोकसà¤à¤¾ मतदारसंघ)",
"note": "multilingual"
},
{
"lang": "ta",
"name": "ஜஹீராபாதà¯",
"note": "multilingual"
},
{
"lang": "te",
"name": "జహీరాబాదౠలోకసఠనియోజకవరà±à°—à°‚",
"note": "multilingual"
}
],
"type": "constituency"
}
]
}
Can any one please help me in doing that. Thanks.
Your json is not an array of roots. The json is a single object with multiple nested objects inside a single root.
{
"posts": [
],
"persons": [
{ "birth_date":
}
Since it's not returning an array of objects, you need to deserialize to a singular root.
var root = JsonConvert.DeserializeObject<Root>(jsonString);
I have the documents in the below format. I would like to count production deployments from the collection. Mongo Playground link is also attached here.
A document is considered as a production deployment when ANY of the following is true.
deployments.steps.environments.name contains Production OR Prod OR Prd
deployments.steps.stages contains Production OR Prod OR Prd
Any help on incorporating the above condition into the query to calculate TotalCount, SucceededCount etc. please?
Update: I have updated the query here. Am I right?
[
{
"productId": "613a5114b24382575e7e7668",
"deployments": [
{
"projectId": "613a5083b24382575e7e765f",
"title": "Release-4",
"steps": [
{
"releaseId": 8168,
"title": "UnitTest-Release-004",
"environments": [
{
"envId": 61553,
"name": "Production"
}
],
"stages": []
},
{
"releaseId": 7376,
"title": "UnitTest-Release-005",
"environments": [],
"stages": [
"Prod"
]
}
]
}
],
"createdAt": ISODate("2021-11-03T07:55:57.486Z"),
"deploymentStatus": "Succeeded",
"completedAt": ISODate("2021-11-03T07:29:00.907Z"),
"startedAt": ISODate("2021-11-03T07:26:53.761Z"),
},
{
"productId": "613a5114b24382575e7e7668",
"deployments": [
{
"projectId": "613a5083b24382575e7e765f",
"title": "Release-4",
"steps": [
{
"releaseId": 8168,
"title": "UnitTest-Release-004",
"environments": [
{
"envId": 61553,
"name": "Production"
}
],
"stages": []
},
{
"releaseId": 7376,
"title": "UnitTest-Release-005",
"environments": [],
"stages": []
}
]
}
],
"createdAt": ISODate("2021-11-03T07:55:57.486Z"),
"deploymentStatus": "Failed",
"completedAt": ISODate("2021-11-03T07:29:00.907Z"),
"startedAt": ISODate("2021-11-03T07:26:53.761Z"),
}
]
Here is the query.
db.collection.aggregate([
{
$match: {
$and: [
{
"createdAt": {
$gte: ISODate("2020-11-01")
}
},
{
"createdAt": {
$lte: ISODate("2021-11-17")
}
}
],
$or: [
{
"deployments.steps.environments.name": {
"$in": [
"Prd",
"Prod",
"Production"
]
}
},
{
"deployments.steps.stages.name": {
"$in": [
"Prd",
"Prod",
"Production"
]
}
}
]
}
},
{
$group: {
_id: "$productId",
TotalCount: {
$sum: 1
},
SucceededCount: {
$sum: {
"$cond": {
"if": {
$eq: [
"$deploymentStatus",
"Succeeded"
]
},
"then": 1,
"else": 0
}
}
},
FailedCount: {
$sum: {
"$cond": {
"if": {
$eq: [
"$deploymentStatus",
"Failed"
]
},
"then": 1,
"else": 0
}
}
},
CancelledCount: {
$sum: {
"$cond": {
"if": {
$eq: [
"$deploymentStatus",
"Cancelled"
]
},
"then": 1,
"else": 0
}
}
},
NotStartedCount: {
$sum: {
"$cond": {
"if": {
$eq: [
"$deploymentStatus",
"NotStarted"
]
},
"then": 1,
"else": 0
}
}
}
}
}
])
MongoPlayground
$cond - if - then - else = without quotes
I'm trying to merge multiple JSON files which has the same type of data inside them and fetch the merged data from those JSONs. For example below are two JSON files.
JSON 1
[
{
"Name": "Sample1",
"Data": [
{
"Name": "Sample1 Sub1",
"Data": [
{
"Name": "XXX",
"ID": ["278924"]
}
]
}
]
},
{
"Name": "Sample2",
"Data": [
{
"Name": "Sample2 Sub1",
"Data": [
{
"Name": "XXX",
"ID": ["278378"]
},
{
"Name": "YYY",
"ID": ["278289"]
}
]
}
]
}
]
JSON 2
[
{
"Name": "Sample1",
"Data": [
{
"Name": "Sample1 Sub1",
"Data": [
{
"Name": "XXX",
"ID": ["357896"]
}
]
}
]
},
{
"Name": "Sample2",
"Data": [
{
"Name": "Sample2 Sub1",
"Data": [
{
"Name": "XXX",
"ID": ["356842"]
},
{
"Name": "YYY",
"ID": ["357123"]
}
]
}
]
}
]
I'm expecting the output to be in the below format.
[
{
"Name": "Sample1",
"Data": [
{
"Name": "Sample1 Sub1",
"Data": [
{
"Name": "XXX",
"ID": ["278924, 357896"]
}
]
}
]
},
{
"Name": "Sample2",
"Data": [
{
"Name": "Sample2 Sub1",
"Data": [
{
"Name": "XXX",
"ID": ["278378,356842"]
},
{
"Name": "YYY",
"ID": ["278289,357123"]
}
]
}
]
}
]
I'm not sure where to start this. I tried groupby for multiple level but couldn't able to fetch the result in expected format.
Any help would be appreciated.
I have some complex JSon that I am trying to parse into something meaningful. I'm attempting to deserialize using C# Json.net but I can't get the values that I need. What I need is the value from every ColData node except those in a "summary" section. I am able to deserialize into an object using but I am stuck there.
string pandltext = #"{
"Header": {
"Time": "2017-08-24T08:32:58-07:00",
"ReportName": "ProfitAndLoss",
"ReportBasis": "Accrual",
"StartPeriod": "2017-06-01",
"EndPeriod": "2017-06-30",
"SummarizeColumnsBy": "Total",
"Currency": "USD",
"Option": [
{
"Name": "AccountingStandard",
"Value": "GAAP"
},
{
"Name": "NoReportData",
"Value": "false"
}
]
},
"Columns": {
"Column": [
{
"ColTitle": "",
"ColType": "Account",
"MetaData": [
{
"Name": "ColKey",
"Value": "account"
}
]
},
{
"ColTitle": "Total",
"ColType": "Money",
"MetaData": [
{
"Name": "ColKey",
"Value": "total"
}
]
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Income"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "Design income",
"id": "82"
},
{
"value": "975.00"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "Discounts given",
"id": "86"
},
{
"value": "-30.50"
}
],
"type": "Data"
},
{
"Header": {
"ColData": [
{
"value": "Landscaping Services",
"id": "45"
},
{
"value": "360.00"
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Job Materials",
"id": "46"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "Fountains and Garden Lighting",
"id": "48"
},
{
"value": "550.00"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "Plants and Soil",
"id": "49"
},
{
"value": "1820.72"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "Sprinklers and Drip Systems",
"id": "50"
},
{
"value": "30.00"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Job Materials"
},
{
"value": "2400.72"
}
]
},
"type": "Section"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Landscaping Services"
},
{
"value": "2760.72"
}
]
},
"type": "Section"
},
{
"ColData": [
{
"value": "Pest Control Services",
"id": "54"
},
{
"value": "-100.00"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "Sales of Product Income",
"id": "79"
},
{
"value": "44.00"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "Services",
"id": "1"
},
{
"value": "400.00"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Income"
},
{
"value": "4049.22"
}
]
},
"type": "Section",
"group": "Income"
},
{
"Summary": {
"ColData": [
{
"value": "Gross Profit"
},
{
"value": "4049.22"
}
]
},
"type": "Section",
"group": "GrossProfit"
},
{
"Header": {
"ColData": [
{
"value": "Expenses"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Automobile",
"id": "55"
},
{
"value": "19.99"
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "Fuel",
"id": "56"
},
{
"value": "179.15"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Automobile"
},
{
"value": "199.14"
}
]
},
"type": "Section"
},
{
"Header": {
"ColData": [
{
"value": "Job Expenses",
"id": "58"
},
{
"value": "108.09"
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Job Materials",
"id": "63"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "Decks and Patios",
"id": "64"
},
{
"value": "88.09"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Job Materials"
},
{
"value": "88.09"
}
]
},
"type": "Section"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Job Expenses"
},
{
"value": "196.18"
}
]
},
"type": "Section"
},
{
"Header": {
"ColData": [
{
"value": "Legal & Professional Fees",
"id": "12"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "Accounting",
"id": "69"
},
{
"value": "75.00"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "Lawyer",
"id": "71"
},
{
"value": "100.00"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Legal & Professional Fees"
},
{
"value": "175.00"
}
]
},
"type": "Section"
},
{
"ColData": [
{
"value": "Maintenance and Repair",
"id": "72"
},
{
"value": "185.00"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "Meals and Entertainment",
"id": "13"
},
{
"value": "5.66"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "Rent or Lease",
"id": "17"
},
{
"value": "900.00"
}
],
"type": "Data"
},
{
"Header": {
"ColData": [
{
"value": "Utilities",
"id": "24"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "Gas and Electric",
"id": "76"
},
{
"value": "114.09"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "Telephone",
"id": "77"
},
{
"value": "74.36"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Utilities"
},
{
"value": "188.45"
}
]
},
"type": "Section"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Expenses"
},
{
"value": "1849.43"
}
]
},
"type": "Section",
"group": "Expenses"
},
{
"Summary": {
"ColData": [
{
"value": "Net Operating Income"
},
{
"value": "2199.79"
}
]
},
"type": "Section",
"group": "NetOperatingIncome"
},
{
"Header": {
"ColData": [
{
"value": "Other Expenses"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "Miscellaneous",
"id": "14"
},
{
"value": "916.00"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Other Expenses"
},
{
"value": "916.00"
}
]
},
"type": "Section",
"group": "OtherExpenses"
},
{
"Summary": {
"ColData": [
{
"value": "Net Other Income"
},
{
"value": "-916.00"
}
]
},
"type": "Section",
"group": "NetOtherIncome"
},
{
"Summary": {
"ColData": [
{
"value": "Net Income"
},
{
"value": "1283.79"
}
]
},
"type": "Section",
"group": "NetIncome"
}
]
}
}
// Deserialize to object
var rootObj = JsonConvert.DeserializeObject<ProfitLoss.Rootobject>( pandltext );
I've tried querying a JContainer like is mentioned in this post. I've tried deserlializing a fragment like is mentioned in the documentation and I've tried using linq as mentioned here in the documentation. So far all of my efforts have met varying degrees of "success" but none have yielded the values I'm trying to get. Eventually this data will be bound to a WPF DataGrid for viewing.
Edit:
Added entire Json file
These are a couple attempts to get something, but I run into null values in both cases.
// This always returns null
var results2 = doc.Descendants()
.OfType<JObject>()
.Where( x => x[ "value" ] != null );
// This gives me a null exception error
var doc1 = ( JContainer ) o[ "Rows" ];
foreach ( var row in rootObj.Rows.Row )
{
// Get a null exception
foreach ( var row2 in row.Rows.Row )
{
Console.WriteLine( row2.ToString() );
}
}
Edit 2:
Using what #Eser gave as a starting point, I am able to get a list of values, but unfortunately it's just a list of values. Instead of getting something like
"Design income", "975.00"
"Discounts given", "-30.50"
I get
"Design income"
"975"
"Discounts given"
"-30.50"
Here is the code I'm using to get a list of values:
var jObj = JObject.Parse( pandltext );
var results = jObj.SelectTokens( "$..Rows.Row[?(#.type == 'Data')]..value" ).ToList();
var jObj = JObject.Parse(json);
var colData = jObj.SelectTokens("$..ColData")
.Except(jObj.SelectTokens("$..Summary.ColData"))
.ToList();
EDIT
foreach(var item in colData)
{
Console.WriteLine(string.Join("=", item.Select(x => x["value"])));
}
or
var finalList = colData.Select(item => item.Select(x => (string)x["value"]).ToList())
.ToList();
public class Option
{
public string Name { get; set; }
public string Value { get; set; }
}
public class Header
{
public DateTime Time { get; set; }
public string ReportName { get; set; }
public string ReportBasis { get; set; }
public string StartPeriod { get; set; }
public string EndPeriod { get; set; }
public string SummarizeColumnsBy { get; set; }
public string Currency { get; set; }
public IList<Option> Option { get; set; }
}
public class MetaData
{
public string Name { get; set; }
public string Value { get; set; }
}
public class Column
{
public string ColTitle { get; set; }
public string ColType { get; set; }
public IList<MetaData> MetaData { get; set; }
}
public class Columns
{
public IList<Column> Column { get; set; }
}
public class ColData
{
public string value { get; set; }
public string id { get; set; }
}
public class ColData
{
public string value { get; set; }
public string id { get; set; }
}
public class Row
{
public IList<ColData> ColData { get; set; }
public string type { get; set; }
}
public class Rows
{
public IList<Row> Row { get; set; }
}
public class ColData
{
public string value { get; set; }
}
public class Summary
{
public IList<ColData> ColData { get; set; }
}
public class ColData
{
public string value { get; set; }
public string id { get; set; }
}
public class Row
{
public Header { get; set; }
public Rows Rows { get; set; }
public Summary Summary { get; set; }
public string type { get; set; }
public IList<ColData> ColData { get; set; }
}
public class Rows
{
public IList<Row> Row { get; set; }
}
public class Row
{
public IList<ColData> ColData { get; set; }
public string type { get; set; }
public Header { get; set; }
public Rows Rows { get; set; }
public Summary { get; set; }
}
public class Rows
{
public IList<Row> Row { get; set; }
}
public class Row
{
public Header { get; set; }
public Rows Rows { get; set; }
public Summary { get; set; }
public string type { get; set; }
public string group { get; set; }
}
public class Rows
{
public IList<Row> Row { get; set; }
}
public class Example
{
public Header Header { get; set; }
public Columns Columns { get; set; }
public Rows Rows { get; set; }
}
and use it with :
Example results = Newtonsoft.JSON.JsonConvert.DeserializeObject<Example>(json);