Get the parent item for a JSON Path

30 views Asked by At

I am using some json path to update name references in fields and it is working great. Now we want a similar thing to return the references. I am trying to modify the same json paths to return the parent items. I am having issues when the matching is not on the same level as the object.

Example JSON Path (This works to update the value)

$.LogicNodeList[*].compChildren[?(@.diagramName == 'S-DGN-B')].diagramName

I want the logicNodeList (this gives an error not liking the '?')

$.LogicNodeList[?(@.compChildren[?(@.diagramName == 'S-DGN-B')])]

Example JSON

    {
    "LogicNodeList": [
        {
          "id": "b61aa8a5-8edd-4207-ab58-ccba83afe67e",
          "name": "CCS_TOP",
          "desc": "",
          "gateType": "gtOr",
          "rootName": "CCS_TOP",
          "compChildren": [],
          "gateChildren": [
            "CCS-Trains",
            "CCS-Supply"
          ],
          "isRoot": true
        },
        {
          "id": "27865e6f-b457-42ff-9303-11c700657212",
          "name": "CCS-Supply",
          "desc": "",
          "gateType": "gtOr",
          "rootName": "CCS_TOP",
          "compChildren": [
            {
              "diagramName": "S-DGN-B",
              "stateValues": []
            },
            {
              "diagramName": "C-MOV-1",
              "stateValues": []
            },
            {
              "diagramName": "S-TNK-T1",
              "stateValues": []
            }
          ],
          "gateChildren": [],
          "isRoot": false
        },
        {
          "id": "6d366c22-5e48-45b5-ac5d-9325a122e451",
          "name": "CCS-Train-A",
          "desc": "",
          "gateType": "gtOr",
          "rootName": "CCS_TOP",
          "compChildren": [
            {
              "diagramName": "C-PMP-A",
              "stateValues": []
            },
            {
              "diagramName": "S-DGN-A",
              "stateValues": []
            },
            {
              "diagramName": "C-CKV-A",
              "stateValues": []
            },
            {
              "diagramName": "C-MOV-A",
              "stateValues": []
            }
          ],
          "gateChildren": [],
          "isRoot": false
        }]
    }

I want the json path to return

    [{
      "id": "27865e6f-b457-42ff-9303-11c700657212",
      "name": "CCS-Supply",
      "desc": "",
      "gateType": "gtOr",
      "rootName": "CCS_TOP",
      "compChildren": [
        {
          "diagramName": "S-DGN-B",
          "stateValues": []
        },
        {
          "diagramName": "C-MOV-1",
          "stateValues": []
        },
        {
          "diagramName": "S-TNK-T1",
          "stateValues": []
        }
      ],
      "gateChildren": [],
      "isRoot": false
    }]
1

There are 1 answers

0
Power Mouse On

i hope you are using C#, (IMHO) otherwise please let me know and i will remove this answer so, why not to select by isroot=false? you will get all children

void Main()
{
    string json = "{\"LogicNodeList\":[{\"id\":\"b61aa8a5-8edd-4207-ab58-ccba83afe67e\",\"name\":\"CCS_TOP\",\"desc\":\"\",\"gateType\":\"gtOr\",\"rootName\":\"CCS_TOP\",\"compChildren\":[],\"gateChildren\":[\"CCS-Trains\",\"CCS-Supply\"],\"isRoot\":true},{\"id\":\"27865e6f-b457-42ff-9303-11c700657212\",\"name\":\"CCS-Supply\",\"desc\":\"\",\"gateType\":\"gtOr\",\"rootName\":\"CCS_TOP\",\"compChildren\":[{\"diagramName\":\"S-DGN-B\",\"stateValues\":[]},{\"diagramName\":\"C-MOV-1\",\"stateValues\":[]},{\"diagramName\":\"S-TNK-T1\",\"stateValues\":[]}],\"gateChildren\":[],\"isRoot\":false},{\"id\":\"6d366c22-5e48-45b5-ac5d-9325a122e451\",\"name\":\"CCS-Train-A\",\"desc\":\"\",\"gateType\":\"gtOr\",\"rootName\":\"CCS_TOP\",\"compChildren\":[{\"diagramName\":\"C-PMP-A\",\"stateValues\":[]},{\"diagramName\":\"S-DGN-A\",\"stateValues\":[]},{\"diagramName\":\"C-CKV-A\",\"stateValues\":[]},{\"diagramName\":\"C-MOV-A\",\"stateValues\":[]}],\"gateChildren\":[],\"isRoot\":false}]}";
    JObject o = JObject.Parse(json);
    var selection = o.SelectTokens("$.LogicNodeList[?(@.isRoot == false)]").Dump();
o.SelectTokens("$.LogicNodeList[*].compChildren[*].diagramName").Dump();
    o.SelectTokens("$.LogicNodeList[*].compChildren[?(@.diagramName == 'S-DGN-B')].diagramName").Dump();
}

enter image description here