Skip to content

Converting input arrays to output arrays starting at a particular index #185

@rishabhdhwaj

Description

@rishabhdhwaj

Input:

{
    "order": {
        "order_items": [
            {
                "id": "1",
                "name": "one"
            },
            {
                "id": "2",
                "name": "two"
            }
        ],
        "default_item": {
            "id": "0",
            "name": "zero"
        }
    }
}

Desired output:

{
  "invoices" : [ {
    "id" : "1",
    "name" : "one"
  }, {
    "id" : "2",
    "name" : "two"
  }, {
    "id" : "0",
    "name" : "zero"
  } ]
}

I am achieving this by using the following spec:

[
    {
        "operation": "shift",
        "spec": {
            "order": {
                "order_items": {
                    "*": {
                        "id": "invoices[&1].id",
                        "name": "invoices[&1].name"
                    }
                },
                "default_item": {
                    "id": "invoices[2].id",
                    "name": "invoices[2].name"
                }
            }
        }
    }
]

But the problem with the above spec is that the input array can have any number of elements.So I can not default the default element to third index like the above spec. I would rather place the default element at the 0 index and start the other elements from input array from index 1 of the output array. How do we do this. Thanks a lot for your help

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions