How can I count the number of properties in a struct or JSON document?











up vote
1
down vote

favorite












I'm making a PDF generator in Go and one of the sections of it will be a table. To create a table I need to state the width of the columns, and this will be done by getting the page width (minus margins) and dividing by the number of columns in the table



The columns in the table are defined in a struct like this:



type Person struct {
Name string `json:"Name"`
Age string `json:"Age"`
Comment string `json:"Comment"`
}


And JSON is unmarshalled into it



I don't want to have to hardcode '3' as the column number into my code and want to know how I can programmatically count the properties either in from the JSON or the struct itself



I've spent a few days searching now, and all results focus on people having trouble getting the values, but I want the keys!



Thanks in advance










share|improve this question






















  • there is no way other than using reflect package, but I strongly suggest change your approach
    – danicheeta
    Nov 19 at 11:01










  • Can I ask why? The PDF package is Gofpdf, to generate a table it basically 'draws' individual cells and then stitches them together, meaning you have to get the individual cell width
    – MassiveOwl
    Nov 19 at 11:06






  • 1




    @danicheeta: Why? Reflection is necessary for this problem anyway.
    – Flimzy
    Nov 19 at 11:17















up vote
1
down vote

favorite












I'm making a PDF generator in Go and one of the sections of it will be a table. To create a table I need to state the width of the columns, and this will be done by getting the page width (minus margins) and dividing by the number of columns in the table



The columns in the table are defined in a struct like this:



type Person struct {
Name string `json:"Name"`
Age string `json:"Age"`
Comment string `json:"Comment"`
}


And JSON is unmarshalled into it



I don't want to have to hardcode '3' as the column number into my code and want to know how I can programmatically count the properties either in from the JSON or the struct itself



I've spent a few days searching now, and all results focus on people having trouble getting the values, but I want the keys!



Thanks in advance










share|improve this question






















  • there is no way other than using reflect package, but I strongly suggest change your approach
    – danicheeta
    Nov 19 at 11:01










  • Can I ask why? The PDF package is Gofpdf, to generate a table it basically 'draws' individual cells and then stitches them together, meaning you have to get the individual cell width
    – MassiveOwl
    Nov 19 at 11:06






  • 1




    @danicheeta: Why? Reflection is necessary for this problem anyway.
    – Flimzy
    Nov 19 at 11:17













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm making a PDF generator in Go and one of the sections of it will be a table. To create a table I need to state the width of the columns, and this will be done by getting the page width (minus margins) and dividing by the number of columns in the table



The columns in the table are defined in a struct like this:



type Person struct {
Name string `json:"Name"`
Age string `json:"Age"`
Comment string `json:"Comment"`
}


And JSON is unmarshalled into it



I don't want to have to hardcode '3' as the column number into my code and want to know how I can programmatically count the properties either in from the JSON or the struct itself



I've spent a few days searching now, and all results focus on people having trouble getting the values, but I want the keys!



Thanks in advance










share|improve this question













I'm making a PDF generator in Go and one of the sections of it will be a table. To create a table I need to state the width of the columns, and this will be done by getting the page width (minus margins) and dividing by the number of columns in the table



The columns in the table are defined in a struct like this:



type Person struct {
Name string `json:"Name"`
Age string `json:"Age"`
Comment string `json:"Comment"`
}


And JSON is unmarshalled into it



I don't want to have to hardcode '3' as the column number into my code and want to know how I can programmatically count the properties either in from the JSON or the struct itself



I've spent a few days searching now, and all results focus on people having trouble getting the values, but I want the keys!



Thanks in advance







json go






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 10:59









MassiveOwl

468313




468313












  • there is no way other than using reflect package, but I strongly suggest change your approach
    – danicheeta
    Nov 19 at 11:01










  • Can I ask why? The PDF package is Gofpdf, to generate a table it basically 'draws' individual cells and then stitches them together, meaning you have to get the individual cell width
    – MassiveOwl
    Nov 19 at 11:06






  • 1




    @danicheeta: Why? Reflection is necessary for this problem anyway.
    – Flimzy
    Nov 19 at 11:17


















  • there is no way other than using reflect package, but I strongly suggest change your approach
    – danicheeta
    Nov 19 at 11:01










  • Can I ask why? The PDF package is Gofpdf, to generate a table it basically 'draws' individual cells and then stitches them together, meaning you have to get the individual cell width
    – MassiveOwl
    Nov 19 at 11:06






  • 1




    @danicheeta: Why? Reflection is necessary for this problem anyway.
    – Flimzy
    Nov 19 at 11:17
















there is no way other than using reflect package, but I strongly suggest change your approach
– danicheeta
Nov 19 at 11:01




there is no way other than using reflect package, but I strongly suggest change your approach
– danicheeta
Nov 19 at 11:01












Can I ask why? The PDF package is Gofpdf, to generate a table it basically 'draws' individual cells and then stitches them together, meaning you have to get the individual cell width
– MassiveOwl
Nov 19 at 11:06




Can I ask why? The PDF package is Gofpdf, to generate a table it basically 'draws' individual cells and then stitches them together, meaning you have to get the individual cell width
– MassiveOwl
Nov 19 at 11:06




1




1




@danicheeta: Why? Reflection is necessary for this problem anyway.
– Flimzy
Nov 19 at 11:17




@danicheeta: Why? Reflection is necessary for this problem anyway.
– Flimzy
Nov 19 at 11:17












2 Answers
2






active

oldest

votes

















up vote
5
down vote



accepted










reflect.TypeOf(Person{}).NumField()


or



 len(structs.Map(Person{}))


(you need to import "github.com/fatih/structs")






share|improve this answer























  • Nice, thanks for this - works great
    – MassiveOwl
    Nov 19 at 11:14


















up vote
-1
down vote













The Object.getOwnPropertyNames(object) will return an array of property name for the given object






share|improve this answer























  • Thanks, i'll try this
    – MassiveOwl
    Nov 19 at 11:06






  • 2




    This question is about Go.
    – Flimzy
    Nov 19 at 11:17











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373171%2fhow-can-i-count-the-number-of-properties-in-a-struct-or-json-document%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
5
down vote



accepted










reflect.TypeOf(Person{}).NumField()


or



 len(structs.Map(Person{}))


(you need to import "github.com/fatih/structs")






share|improve this answer























  • Nice, thanks for this - works great
    – MassiveOwl
    Nov 19 at 11:14















up vote
5
down vote



accepted










reflect.TypeOf(Person{}).NumField()


or



 len(structs.Map(Person{}))


(you need to import "github.com/fatih/structs")






share|improve this answer























  • Nice, thanks for this - works great
    – MassiveOwl
    Nov 19 at 11:14













up vote
5
down vote



accepted







up vote
5
down vote



accepted






reflect.TypeOf(Person{}).NumField()


or



 len(structs.Map(Person{}))


(you need to import "github.com/fatih/structs")






share|improve this answer














reflect.TypeOf(Person{}).NumField()


or



 len(structs.Map(Person{}))


(you need to import "github.com/fatih/structs")







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 at 11:23

























answered Nov 19 at 11:12









Ehsan.Saradar

41827




41827












  • Nice, thanks for this - works great
    – MassiveOwl
    Nov 19 at 11:14


















  • Nice, thanks for this - works great
    – MassiveOwl
    Nov 19 at 11:14
















Nice, thanks for this - works great
– MassiveOwl
Nov 19 at 11:14




Nice, thanks for this - works great
– MassiveOwl
Nov 19 at 11:14












up vote
-1
down vote













The Object.getOwnPropertyNames(object) will return an array of property name for the given object






share|improve this answer























  • Thanks, i'll try this
    – MassiveOwl
    Nov 19 at 11:06






  • 2




    This question is about Go.
    – Flimzy
    Nov 19 at 11:17















up vote
-1
down vote













The Object.getOwnPropertyNames(object) will return an array of property name for the given object






share|improve this answer























  • Thanks, i'll try this
    – MassiveOwl
    Nov 19 at 11:06






  • 2




    This question is about Go.
    – Flimzy
    Nov 19 at 11:17













up vote
-1
down vote










up vote
-1
down vote









The Object.getOwnPropertyNames(object) will return an array of property name for the given object






share|improve this answer














The Object.getOwnPropertyNames(object) will return an array of property name for the given object







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 at 14:14









AmerllicA

2,90211033




2,90211033










answered Nov 19 at 11:04









Shiju

1533




1533












  • Thanks, i'll try this
    – MassiveOwl
    Nov 19 at 11:06






  • 2




    This question is about Go.
    – Flimzy
    Nov 19 at 11:17


















  • Thanks, i'll try this
    – MassiveOwl
    Nov 19 at 11:06






  • 2




    This question is about Go.
    – Flimzy
    Nov 19 at 11:17
















Thanks, i'll try this
– MassiveOwl
Nov 19 at 11:06




Thanks, i'll try this
– MassiveOwl
Nov 19 at 11:06




2




2




This question is about Go.
– Flimzy
Nov 19 at 11:17




This question is about Go.
– Flimzy
Nov 19 at 11:17


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373171%2fhow-can-i-count-the-number-of-properties-in-a-struct-or-json-document%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Feedback on college project

Futebolista

Albești (Vaslui)