Home > Software design >  How to parse a json which contains spaces in keys in java
How to parse a json which contains spaces in keys in java

Time:08-22

My json response is like this

{
    "stats": {
        "Fortify": {
            "name": "Fortify",
            "count": 167,
            "stat": {
                "Critical": {
                    "name": "Critical",
                    "count": 161,
                    "stat": {}
                },
                "High": {
                    "name": "High",
                    "count": 5,
                    "stat": {}
                },
                "Low": {
                    "name": "Low",
                    "count": 1,
                    "stat": {}
                }
            }
        },
        "WhiteHat Sentinel": {
            "name": "WhiteHat Sentinel",
            "count": 152,
            "stat": {
                "High": {
                    "name": "High",
                    "count": 69,
                    "stat": {}
                },
                "Critical": {
                    "name": "Critical",
                    "count": 36,
                    "stat": {}
                },
                "Medium": {
                    "name": "Medium",
                    "count": 31,
                    "stat": {}
                },
                "Low": {
                    "name": "Low",
                    "count": 16,
                    "stat": {}
                }
            }
        },
        "HackerOne": {
            "name": "HackerOne",
            "count": 51,
            "stat": {
                "High": {
                    "name": "High",
                    "count": 19,
                    "stat": {}
                },
                "Critical": {
                    "name": "Critical",
                    "count": 15,
                    "stat": {}
                },
                "Medium": {
                    "name": "Medium",
                    "count": 14,
                    "stat": {}
                },
                "Low": {
                    "name": "Low",
                    "count": 3,
                    "stat": {}
                }
            }
        },
        "Netsparker": {
            "name": "Netsparker",
            "count": 37,
            "stat": {
                "Medium": {
                    "name": "Medium",
                    "count": 15,
                    "stat": {}
                },
                "High": {
                    "name": "High",
                    "count": 10,
                    "stat": {}
                },
                "Low": {
                    "name": "Low",
                    "count": 10,
                    "stat": {}
                },
                "Critical": {
                    "name": "Critical",
                    "count": 2,
                    "stat": {}
                }
            }
        },
        "Contrast Security": {
            "name": "Contrast Security",
            "count": 7,
            "stat": {
                "Medium": {
                    "name": "Medium",
                    "count": 4,
                    "stat": {}
                },
                "Critical": {
                    "name": "Critical",
                    "count": 1,
                    "stat": {}
                },
                "High": {
                    "name": "High",
                    "count": 1,
                    "stat": {}
                },
                "Low": {
                    "name": "Low",
                    "count": 1,
                    "stat": {}
                }
            }
        }
    },
    "totalDocCount": null
}

as you can see there are two keys where there is a space between them that are "Whitehat Sentinental" and "Contrast security".

Whenever I try to parse it normally as

 secondToolHighSeverity = response.getBody().jsonPath().getInt("stats.WhiteHat Sentinel.stat.High.count");
  secondToolCriticalSeverity = response.getBody().jsonPath().getInt("stats.WhiteHat Sentinel.stat.Critical.count");

It throws illegal argument exception: The parameter "Sentinel" was used but not defined. Define parameters using the JsonPath.params(...) function Can someone tell me how to parse this

CodePudding user response:

There are two ways of accessing properties;

  1. foo.bar
  2. foo['bar']

you need to use the second option to use spaces for example foo['bar bar'].baz

  • Related