Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not return value when a period/dot is in key even if escaped #355

Open
eexbee opened this issue Apr 18, 2024 · 1 comment
Open

Can not return value when a period/dot is in key even if escaped #355

eexbee opened this issue Apr 18, 2024 · 1 comment

Comments

@eexbee
Copy link

eexbee commented Apr 18, 2024

go1.18.1 linux/amd64
gjson version 1.17.1

sample json:

{
"timestamp": "2024-04-17T05:39:04",
"MAIN.uptime": {
    "description": "Child process uptime",
    "flag": "c", "format": "d",
    "value": 320
  },

  "MAIN.sess_conn": {
    "description": "Sessions accepted",
    "flag": "c", "format": "i",
    "value": 8
  },

  "MAIN.sess_drop": {
    "description": "Sessions dropped",
    "flag": "c", "format": "i",
    "value": 0
  },
...
}

I want to retrieve the value inside MAIN.sess_conn, I've tried all this and it prints empty string:

fmt.Println(gjson.Get(json, gjson.Escape("MAIN.sess_conn")))  // prints blank line
fmt.Println(gjson.Get(json, `MAIN\.sess_conn.flag`))  // prints blank line
fmt.Println(gjson.Get(json, `MAIN\.sess_conn.value`).String())  // prints blank line

Even the Exists method returns false

fmt.Println(gjson.Get(json, `MAIN\.sess_conn.value`).Exists())  // prints false
fmt.Println(gjson.Get(json, `MAIN\.sess_conn`).Exists())  // prints false
@tidwall
Copy link
Owner

tidwall commented Apr 20, 2024

It all works for me.

json := `{
	"timestamp": "2024-04-17T05:39:04",
	"MAIN.uptime": {
		"description": "Child process uptime",
		"flag": "c", "format": "d",
		"value": 320
		},
	
		"MAIN.sess_conn": {
		"description": "Sessions accepted",
		"flag": "c", "format": "i",
		"value": 8
		},
	
		"MAIN.sess_drop": {
		"description": "Sessions dropped",
		"flag": "c", "format": "i",
		"value": 0
		}
	}
}`
fmt.Println(gjson.Get(json, gjson.Escape("MAIN.sess_conn"))) // prints blank line
fmt.Println(gjson.Get(json, `MAIN\.sess_conn.flag`)) // prints blank line
fmt.Println(gjson.Get(json, `MAIN\.sess_conn.value`).String()) // prints blank line

// Output:
// {
// 		"description": "Sessions accepted",
// 		"flag": "c", "format": "i",
// 		"value": 8
// 		}
// c
// 8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants