Java flatten json文档

时间:2017-05-04 09:48:55

标签: java json flatten spagobi

我是Java的新手,我正在寻找一种平整json文档的方法。

我已经尝试过Object mapper但没有成功,我也尝试过使用json节点,但仍然没有成功。

我找到了这个链接,但结果不是我需要的:https://github.com/wnameless/json-flattener

之前我也得到过帮助,但是这个例子太具体了,我不能做同样的事情,因为文件太长了,这就是为什么我正在寻找一种让它变得通用的方法:Flatten json documents in Java

我需要改变"任何" json文档如下例所示:

以下是我的文档示例

收到的文件:

{ 
  "took": 7, 
  "timed_out": false, 
  "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
  }, 
  "hits": { 
    "total": 10, 
    "max_score": 0, 
    "hits": [] 
  }, 
  "aggregations": { 
    "groupe": { 
      "doc_count_error_upper_bound": 0, 
      "sum_other_doc_count": 0, 
      "buckets": [ 
        { 
          "key": "a", 
          "doc_count": 1, 
          "date": { 
            "buckets": [ 
              { 
                "key_as_string": "2017-05-03T00:00:00.000Z", 
                "key": 1493769600000, 
                "doc_count": 1, 
                "value": { 
                  "value": 1 
                } 
              }, 
              { 
                "key_as_string": "2017-05-03T01:00:00.000Z", 
                "key": 1493776800000, 
                "doc_count": 1, 
                "value": { 
                  "value": 3 
                } 
              } 
            ] 
          } 
        }, 
        { 
          "key": "b", 
          "doc_count": 4, 
          "date": { 
            "buckets": [ 
              { 
                "key_as_string": "2017-05-03T00:00:00.000Z", 
                "key": 1493769600000, 
                "doc_count": 1, 
                "value": { 
                  "value": 4 
                } 
              }, 
              { 
                "key_as_string": "2017-05-03T01:00:00.000Z", 
                "key": 1493773200000, 
                "doc_count": 1, 
                "value": { 
                  "value": 3 
                } 
              } 

            ] 
          } 
        } 
      ] 
    } 
  } 
} 

转换文件:

{ 
  "took": 7, 
  "timed_out": false, 
  "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
  }, 
  "hits": { 
    "total": 10, 
    "max_score": 0, 
    "hits": [] 
  }, 
  "aggregations": { 
    "groupe": { 
      "doc_count_error_upper_bound": 0, 
      "sum_other_doc_count": 0, 
      "buckets": [ 
        { 
          "key": "a", 
          "doc_count": 1, 
          "date": { 
            "buckets": [ 
              { 
                "key_as_string": "2017-05-03T00:00:00.000Z", 
                "key": 1493769600000, 
                "doc_count": 1, 
                "value": { 
                  "value": 1 
                } 
              } 
            ] 
          } 
        }, 
        { 
          "key": "a", 
          "doc_count": 1, 
          "date": { 
            "buckets": [ 
              { 
                "key_as_string": "2017-05-03T02:00:00.000Z", 
                "key": 1493776800000, 
                "doc_count": 1, 
                "value": { 
                  "value": 3 
                } 
              } 
            ] 
          } 
        }, 
        { 
          "key": "b", 
          "doc_count": 1, 
          "date": { 
            "buckets": [ 
              { 
                "key_as_string": "2017-05-03T02:00:00.000Z", 
                "key": 1493776800000, 
                "doc_count": 1, 
                "value": { 
                  "value": 4 
                } 
              } 
            ] 
          } 
        }, 

 "key": "b", 
          "doc_count": 1, 
          "date": { 
            "buckets": [ 
              { 
                "key_as_string": "2017-05-03T02:00:00.000Z", 
                "key": 1493776800000, 
                "doc_count": 1, 
                "value": { 
                  "value": 4 
                } 
              } 
            ] 
          } 
        } 


      ] 
    } 
  } 
}  

0 个答案:

没有答案