Add sql and rework json
do some sql magics