Skip to content

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

@madhurikoduru

Description

@madhurikoduru

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions