tidwall / gjson

Get JSON values quickly - JSON parser for Go
MIT License
14.1k stars 846 forks source link

gojson.tostr method missing double quotes #223

Closed almas1992 closed 3 years ago

almas1992 commented 3 years ago

I have a json string like

{
  "data": [
    {
      "name": "Product P4",
      "productId": "1bb3",
      "vendorId": "10de"
    },
    {
      "name": "Product P4",
      "productId": "1cc3",
      "vendorId": "20de"
    },
    {
      "name": "Product P4",
      "productId": "1dd3",
      "vendorId": "30de"
    }
  ]
}

and path syntax data.#.{name,value:{productId,vendorId}} wil get data

[
  {
    "name": "Product P4",
    "value": {
      "productId": "1bb3",
      "vendorId": "10de"
    }
  },
  {
    "name": "Product P4",
    "value": {
      "productId": "1cc3",
      "vendorId": "20de"
    }
  },
  {
    "name": "Product P4",
    "value": {
      "productId": "1dd3",
      "vendorId": "30de"
    }
  }
]

but I want to get data like this

[
  {
    "name": "Product P4",
    "value": "{\"productId\":\"1bb3\",\"vendorId\":\"10de\"}"
  },
  {
    "name": "Product P4",
    "value": "{\"productId\":\"1cc3\",\"vendorId\":\"20de\"}"
  },
  {
    "name": "Product P4",
    "value": "{\"productId\":\"1dd3\",\"vendorId\":\"30de\"}"
  }
]

so I did add the modifier

gjson.AddModifier("string", func(josn, arg string) string {

        return strconv.Quote(josn)
    })

and get path syntax data.#.{name,value:{productId,vendorId}.@string} . However, after the gojson.tostr method, it looks like this

[
  {
    "name": "Product P4",
    "value": "{\"productId\":\"1bb3\",\"vendorId\":\"10de\"}
  },
  {
    "name": "Product P4",
    "value": "{\"productId\":\"1cc3\",\"vendorId\":\"20de\"}
  },
  {
    "name": "Product P4",
    "value": "{\"productId\":\"1dd3\",\"vendorId\":\"30de\"}
  }
]

Why value lost the last double quotes?

tidwall commented 3 years ago

You found a bug. Thanks for reporting. I pushed a fix.