# Parsing JSON structure dynamiclly



## kr651129 (Jul 21, 2013)

I know this isn't a FreeBSD specific question but I'm hoping to get some help from others who may have ran into the same problem.  I'm writing a Java application that gets some JSON data that I need to parse and I'm having a difficult time with it, I'm hoping someone here could shed some light.

The problem I'm having is I never know what the JSON data will look like beforehand.  I need to get the JSON structure dynamically.  Take the following pseudo-JSON as an example:


```
{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}
```

I found a library called JSON-Path that will allow me to call values from the above object similar to x-path with XML.  The problem: I need to know the structure of the above object to be able to use JSON-Path.

Ideally I'd like to have it broken down in an ArrayList.  Maybe using RegEx?  Any insight to this problem would be helpful.

Thank you!

Edit:
Sorry if I'm not using the right JSON terms I've not worked with it much in the past, and I can't stand this format.


----------



## expl (Jul 22, 2013)

Check out JSONlib. It can parse the JSON string to an abstract object that you can then analyze programmatically.

Should not hate JSON, hate your JSON support . I prefer JSON serialization over any other format, because it is quite compact and well supported.


----------



## kr651129 (Jul 22, 2013)

Thanks for the reply.  I downloaded the library and will play with it today.  If you don't mind, let me ask you a couple more things about JSON since you sound like you know what you're doing with it.  I've had a hard time coming across good help for what I'm trying to do.

From what I understand if I want to get a JSON object and parse it correctly I have to explicitly create a POJO that matches the JSON I'm receiving.  As you can imagine this is a problem for me since I don't know what the data is ahead of time.

Using the same JSON example as I posted, the best I've been able to achieve is getting the first object name, "glossary" and then "glossdiv", everything inside of "glossdiv" returns as one string.  Then I try to continue parsing the strings returned but I get a malformed JSON object.

Just doing a quick once over of the library you suggested it seams as though it's fairly similar to the others I've tried in that I need to create a POJO with the JSON format in mind.

I figured if I (using the same JSON I posed) could create some sort of Map that would breakdown the hierarchy of the JSON I could using the JSON-Path library to call the data I need since I'll have the structure saved without having to create a strict POJO.

Any advice/JSON expertise it welcome.  And again sorry if the vocab since I'm only a few weeks into working with JSON.


----------



## expl (Jul 23, 2013)

In http://json-lib.sourceforge.net/usage.html look for "Convert to DynaBean:" example. I think this is what you need, no? You parse JSON to an abstract object that you can then parse and do dynamic type casting to Java native types depending on JSON field type.


----------

