Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to parse a json which contains spaces in keys in java

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 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

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading