From d498de1770df8f82df6a907f6a3df4107be97edd Mon Sep 17 00:00:00 2001 From: Josh Baker Date: Sun, 21 Aug 2016 07:30:33 -0700 Subject: [PATCH 1/3] Update README.md --- README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/README.md b/README.md index 379ba92..0b15e33 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,37 @@ result.Type // can be String, Number, True, False, Null, or JSON result.Str // holds the string result.Num // holds the float64 number result.Raw // holds the raw json +result.Multi // holds nested array values +``` + +## Get nested array values + +Suppose you want all the last names from the following json: + +```json +{ + "programmers": [ + { + "firstName": "Janet", + "lastName": "McLaughlin", + }, { + "firstName": "Elliotte", + "lastName": "Hunter", + }, { + "firstName": "Jason", + "lastName": "Harold", + } + ] +}` +``` + +You would use the path "programmers.#.lastName" like such: + +```go +result := gjson.Get(json, "programmers.#.lastName") +for _,name := range result.Multi { + println(name.String()) +} ``` ## Check for the existence of a value From 360ac81635184526bf5bf6c5ef45b186a8030ad5 Mon Sep 17 00:00:00 2001 From: Josh Baker Date: Sun, 21 Aug 2016 09:56:42 -0700 Subject: [PATCH 2/3] additional example --- README.md | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0b15e33..6686338 100644 --- a/README.md +++ b/README.md @@ -48,11 +48,14 @@ This will print: Prichard ``` -A path is a series of keys separated by a dot. -A key may contain special wildcard characters '\*' and '?'. -To access an array value use the index as the key. +## Path Syntax + +A path is a series of keys separated by a dot. +A key may contain special wildcard characters '\*' and '?'. +To access an array value use the index as the key. To get the number of elements in an array or to access a child path, use the '#' character. The dot and wildcard characters can be escaped with '\'. + ``` { "name": {"first": "Tom", "last": "Anderson"}, @@ -64,6 +67,7 @@ The dot and wildcard characters can be escaped with '\'. {"first": "Roger", "last": "Craig"} ] } + "name.last" >> "Anderson" "age" >> 37 "children.#" >> 3 @@ -72,11 +76,9 @@ The dot and wildcard characters can be escaped with '\'. "c?ildren.0" >> "Sara" "fav\.movie" >> "Deer Hunter" "friends.#.first" >> [ "James", "Roger" ] +"friends.1.last" >> "Craig" ``` - - - ## Result Type GJSON supports the json types `string`, `number`, `bool`, and `null`. From e42a0961e6bd0f8994b47bddc3f11e93aaa04b8e Mon Sep 17 00:00:00 2001 From: Josh Baker Date: Sun, 21 Aug 2016 10:56:31 -0700 Subject: [PATCH 3/3] added go tag to codeblock --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6686338..edf9859 100644 --- a/README.md +++ b/README.md @@ -147,7 +147,7 @@ for _,name := range result.Multi { Sometimes you may want to see if the value actually existed in the json document. -``` +```go value := gjson.Get(json, "name.last") if !value.Exists() { println("no last name")