专栏名称: 狗厂
目录
相关文章推荐
新闻广角  ·  网络“大V”司马南偷税被罚超900万 ·  16 小时前  
新闻广角  ·  马航370航班客机残骸搜寻获批重启 ·  昨天  
新闻广角  ·  滞留太空超9个月,两名美国宇航员返回地球 ·  2 天前  
新闻广角  ·  央视主持人康辉,有新身份! ·  3 天前  
51好读  ›  专栏  ›  狗厂

[英]如何用Go访问深层嵌套的JSON数据

狗厂  · 掘金  ·  · 2018-07-06 08:55

正文

Most often developer needs to consume JSON data from other service and query over them. Querying JSON document is little time-consuming. For the last few days, I was working on a package for Golang to query JSON data easily. The idea and inspiration come from PHP-JSONQ by Nahid Bin Azhar.

Let's take a sample JSON data to start with:

{
   "name":"computers",
   "description":"List of computer products",
   "vendor":{
      "name":"Star Trek",
      "email":"[email protected]",
      "website":"www.example.com",
      "items":[
         {"id":1, "name":"MacBook Pro 13 inch retina","price":1350},
         {"id":2, "name":"MacBook Pro 15 inch retina", "price":1700},
         {"id":3, "name":"Sony VAIO", "price":1200},
         {"id":4, "name":"Fujitsu", "price":850},
         {"id":5, "name":"HP core i5", "price":850, "key": 2300},
         {"id":6, "name":"HP core i7", "price":950},
         {"id":null, "name":"HP core i3 SSD", "price":850}
      ],
      "prices":[
         2400,
         2100,
         1200,
         400.87,
         89.90,
         150.10
     ]
   }
}

Let's find a deeply nested property and handle error properly, in this case, we'll try to access name from the second element of items array, note: items is a property of vendor object. See the example below:

package main

import (
    "fmt"
    "log"

    "github.com/thedevsaddam/gojsonq"
)

func main() {
    jq := gojsonq.New().File("./sample-data.json")
    res := jq.Find("vendor.items.[1].name")

    if jq.Error() != nil {
        log.Fatal(jq.Errors())
    }

    fmt.Println(res)
}

Yahooooo! Very simple right? It looks like working with ORM of JSON data. Let's see some more example to query over the sample data.

Example 1

Query: select * from vendor.items where price > 1200 or id null

Using gojsonq we can do the query like:

package main

import (
    "fmt"

    "github.com/thedevsaddam/gojsonq"
)

func main() {
    jq := gojsonq.New().File("./sample-data.json")
    res := jq.From("vendor.items").Where("price", ">", 1200).OrWhere("id", "=", nil).Get()
    fmt.Println(res)
    // output: [map[price:1350 id:1 name:MacBook Pro 13 inch retina] map[id:2 name:MacBook Pro 15 inch retina price:1700] map[id:<nil> name:HP core i3 SSD price:850]]
}

Example 2

Query: select name, price from vendor.items where price > 1200 or id null

Using gojsonq we can do the query like:

package main

import (
    "fmt"

    "github.com/thedevsaddam/gojsonq"
)

func main() {
    jq := gojsonq.New().File("./sample-data.json")
    res := jq.From("vendor.items").Where("price", ">", 1200).OrWhere("id", "=", nil).Only("name", "price")
    fmt.Println(res)
    // output: [map[name:MacBook Pro 13 inch retina price:1350] map[name:MacBook Pro 15 inch retina price:1700] map[name:HP core i3 SSD price:850]]
}

Example 3

Query: select sum(price) from vendor.items where price > 1200 or id null

Using gojsonq we can do the query like:

package main

import (
    "fmt"

    "github.com/thedevsaddam/gojsonq"
)

func main() {
    jq := gojsonq.New().File("./sample-data.json")
    res := jq.From("vendor.items").Where("price", ">", 1200).OrWhere("id", "=", nil).Sum("price")
    fmt.Println(res)
    // output: 3900
}

Example 4

Query: select price from vendor.items where price > 1200







请到「今天看啥」查看全文