Marshal icon indicating copy to clipboard operation
Marshal copied to clipboard

Add possibility to use indexes for array objects nested by keypath

Open rafalwojcik opened this issue 5 years ago • 2 comments

This PR adds a new feature of getting the object at a particular index by keyPath. Let's say that we have JSON like this:

{
   "id": 5393,
   "title": "",
   "content": "",
   "categories": [
       {
          "id": 321
          "name": "Main Category"
       }, {
          "id": 321
          "name": "Child Category"
       },
   ]
}

Right now is not possible to parse just first object from category array. After my change you can use index in keypath like this: category = try object.value(for: "categories.0") this will take a look inside array if object at key categories is array if not it will raise exception or follow keyPath as string value of 0.

rafalwojcik avatar Jan 03 '19 16:01 rafalwojcik

I think there's precedent for specifying the index inside square brackets.

JetForMe avatar Apr 02 '19 05:04 JetForMe

@JetForMe no need to specify square brackets. If a nested object is not array it will start looking for 0 as key. I think it is the right behavior.

rafalwojcik avatar Aug 09 '19 15:08 rafalwojcik