2

I'm trying to take an array of objects from json file and i have an issue.

path.get("wgcTournaments.items")

What path i should use to get all items(item0, item1, item2 ...) in items?

Can you please give me an advice how to do it.

Json example

{
  "wgcTournaments": {
    "items": {
      "jcr:primaryType": "nt:unstructured",
      "item0": {
        "jcr:primaryType": "nt:unstructured",
        "test": "test",
        "test1": "test1"
      },
      "item1": {
        "jcr:primaryType": "nt:unstructured",
        "test": "test",
        "test1": "test1"
      },
      "item2": {
        "jcr:primaryType": "nt:unstructured",
        "test": "test",
        "test1": "test1"
      },
      "item3": {
        "jcr:primaryType": "nt:unstructured",
        "test": "test",
        "test1": "test1"
      }
    }
  }
}

The best way to filter item from items object but i don't understand how to do it with json path.

tom redfern
  • 30,562
  • 14
  • 91
  • 126
Roman
  • 91
  • 1
  • 10

2 Answers2

2

Finally i found a solution for my question.

If you want to get item from items you need to use this one json Path

path.getObject("wgcTournaments.items*.
find{it.key.startsWith('item')}.value",ItemClass[].class);

Note: it was RestAssured and he uses Gpath more details you can find here http://docs.groovy-lang.org/latest/html/documentation/#_gpath

Roman
  • 91
  • 1
  • 10
0

You are trying to deserialize an object into an array of objects. Either your code or your JSON (most likely) is wrong.

If you want to deserialize items as an array, your JSON should be the following:

{
  "wgcTournaments": {
    "items": [
        {
          "jcr:primaryType": "nt:unstructured",
          "item0": {},
          "item1": {},
          "item2": {},
          "item3": {}
        }
    ]
  }
}

Otherwise, if your JSON is correct, you should deserialize your JSON using the following line:

path.getObject("wgcTournaments.items", MyClass.class)

EDIT: After your edit, this seems to be what you want:

If your JSON in correct and you indeed want an array, I assume that each itemX is a the key and {} the corresponding value. In this case, you have to know that you cannot have an associative array in JSON, you should use a custom solution to deserialize it, because your associative array will be converted into an object.

AntoineB
  • 4,535
  • 5
  • 28
  • 61
  • Can you please suggest me how i can get all item from items with json path? Maybe something like wgcTournaments.items.findAll {item ...} – Roman Dec 19 '16 at 13:16
  • You cannot get it directly. You might be able to declare something like a converter that would handle the data but you'll have to look for how to do it, I'm not very experienced with it. – AntoineB Dec 19 '16 at 13:21
  • Sorry i edited question I need a json path not a way to convert it. – Roman Dec 19 '16 at 13:24
  • Then I don't think you can, you can't get a path to an array if your JSON is not an array I believe. – AntoineB Dec 19 '16 at 13:28