1

I have data in JSON format:

{
   "orders":[
      {
         "id":"1",
         "price":720,
         "customers":[
            {
               "id":"10",
               "name":"customer A" 
            },
            {
               "id":"14",
               "name": "customer B"
            }
         ]
      },
      {
         "id":"2",
         "price":68,
         "customers":[
            {
               "id":"7",
               "name":"customer X"
            },
            {
               "id":"8",
               "name":"customer Y"
            }
         ]
      }
   ]
}

My task is go throught these data and create table with columns like for example orders.id, orders.number, orders.customers.id and so on (and of course with data from right side) and with all permutations for nested objects.

So in this case I should got something like:

+-----------+--------------+---------------------+-----------------------+
| orders.id | orders.price | orders.customers.id | orders.customers.name |
+-----------+--------------+---------------------+-----------------------+
|         1 |          720 |                  10 | customer A            |
|         1 |          720 |                  14 | customer B            |
|         2 |           68 |                   7 | customer X            |
|         2 |           68 |                   8 | customer Y            |
+-----------+--------------+---------------------+-----------------------+

The main problem is that I don't know the structure of this file when loading it. So I don't know the type, name and number of attributes and nesting level. I don't know what classes I should keep data in.

I tried use this: https://stackoverflow.com/a/58009049/11846473 to flatten my Json, but I'm not sure if this is good idea...

I would like use Jaskson library to do this. Could you give me some hints?

mosiek
  • 131
  • 1
  • 9
  • Why exactly can't you use the code you've referenced? – Lino Jul 09 '20 at 11:50
  • Because these code only flattern Json. I can not then make permutations and I'm loosing information about nested nodes (which one belongs to parent? How many elements has?). Eventually I can parse names of nodes in output but in my opinion that is not good idea...Maybe you can show how can I use code from flattering Json? – mosiek Jul 09 '20 at 12:10
  • Can you maybe be more specific about your input json? Where does it come from? Do you only have to display it in a fancy table? Etc. – Lino Jul 09 '20 at 12:12
  • I am getting this Json as ```byte[]``` so I am reading it as ```ObjectMapper mapper = new ObjectMapper();``` ```JsonNode root = mapper.readTree(file);``` I need to store my results in ```List``` where every Row is simple ```Map```. As key is comma separeted column names and value is coma separeted values so in my case for example (for first row): ```<"orders.id, orders.price, orders.customers.id, orders.customers.name", "1, 720, 10, customer A" >``` – mosiek Jul 09 '20 at 12:24

0 Answers0