3

If I have JSON document like this

[
    {
        "number" : "650-462-9154",
        "type" : "main"
    },
    {
        "number" : "650-462-1252",
        "type" : "fax"
    }
]

What JSONPath can I use to get the array length (which is 2), without hardcoding any property values?

Using the tool I have, here is some examples they gave, which doesn't help me figure out what value I need.

[
  { 
    "type": "add",
    "id":   "tt0484562",
    "version": 1,
    "lang": "en",
    "fields": {
      "title": "The Seeker: The Dark Is Rising",
      "director": "Cunningham, David L.",
      "genre": ["Adventure","Drama","Fantasy","Thriller"],
      "actor": ["McShane, Ian","Eccleston, Christopher","Conroy, Frances",
                "Crewson, Wendy","Ludwig, Alexander","Cosmo, James",
                "Warner, Amelia","Hickey, John Benjamin","Piddock, Jim",
                "Lockhart, Emma"]
    }
  },
  { 
    "type": "delete",
    "id":   "tt0484575",
    "link_ref": null,
    "version": 2
  }
]

$.[0].genre ---> 0
$.[0].fields.genre ---> 1
$.[0].fields.genre[*] ---> 4
$.[*].type ---> 2
$.[1].link_ref ---> 1
3
  • What is the definition of GetJSONValueCount? Commented Aug 20, 2018 at 15:49
  • Added too much info, made it cleaner now. Commented Aug 20, 2018 at 15:51
  • The program I use, accepts it in that format. Commented Aug 20, 2018 at 15:52

1 Answer 1

3

You can get the length of the array from the first example by using the .length() function like this :

$.length

That should return something like this:

[
    2
]

As for the second example, you can access any array keys using regular dot notation ($.node1.node2.node3) and call the length() function on the node key you wish to get the length of. For example, if you wanted to get the number of values in the actor array you could do something like:

$..actor.length

Which will return something like this:

[
    10
]

Tested on https://codebeautify.org/jsonpath-tester and http://jsonpath.com/ .

You can find more functions in the jsonpath github repo.

Sign up to request clarification or add additional context in comments.

2 Comments

where are the valid jsonpath functions are documented?
"$.length()" solved the issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.