Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finding whether the keys of the input json is a subset of a array of string values. #124

Open
madhurikoduru opened this issue Jul 19, 2022 · 1 comment

Comments

@madhurikoduru
Copy link

Hi, I am trying to find whether the keys of the input json file is a subset of a array of string values. This is my input json file.
{
"ActionId": "PU",
"ActionName": "InsertACRDecision",
"BCAssetId": "54bc8609-6830-46bf-b452-2cefe7436d1b",
"BCAssetType": "ChangeRequest",
"ProjectBCAssetId": "64d3387a-daaa-4ade-ab0a-da99a9dfd5f0",
"AttributesToUpdate":[ {
"CRSubmissionTime": "2021-03-15T12:13:22+06:00",
"CRDecisions": [
{
"CRDecisionTime": "2022-02-11T11:11:08+06:00",
"CRDecisionNum": "1",
"CRDecisionStatus": "approve"
}
]
}
]
}
And this is my method, where AA is the string version of input json file . I am returning a bool value and a message which shows whether its failed or not.
func checkAttributesToUpdate(AA string) (bool, []Msg) {
AAObj, err := gabs.ParseJSON([]byte(AA)) //I am parsing the input json file
if err != nil {
panic(err)
}
result := true
Attr := AAObj.Path("AttributesToUpdate")
AttrElements := Attr.Children()
var errormsg []Msg
for _, Attr := range AttrElements {
if !Attr.Exists("CRDecisions") || !Attr.Exists("CRSubmissionTime") {
message := Msg{"Attributes to update got voilated for partial update"}
errormsg = append(errormsg, message)
result = false
return result, errormsg
}
}
return result, errormsg
}
So exactly right here I should fetch the key values of attributes to update from the input json and check whether they are in set of a array of values. So I have to define "CRDecisions, CRSubmissionTime in an array and then compare the values of json with the array of values by looping through it and find out whether its a subset or not.
Actually , the above code is working fine but if I add a new attribute to update in json input file it is also passing the method with a true value in that case too. Can you please help me how I can compare the keys with array of string values.
"AttributesToUpdate":[ {
"CRSubmissionTime": "2021-03-15T12:13:22+06:00",
"CRDecisions": [
{
"CRDecisionTime": "2022-02-11T11:11:08+06:00",
"CRDecisionNum": "1",
"CRDecisionStatus": "approve"
}
]
} this is the main part of my json file which I should concentrate for this code.

@mihaitodor
Copy link
Contributor

Hey @madhurikoduru, not sure if you still need help with this, but I'm having a really hard time understanding what you're looking to get... If it's still an issue, please provide a clear and self-contained example that I can run myself and please try to format your code so I can copy it without indentation issues. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants