1

I have a JSON Payload and I would like to use the name of each repeating object and add it to the body :

{
  "error": [],
  "result": {
    "trades": {
      "foo-test-UCI7BS": {
        "ordertxid": "00TEST",
        "postxid": "00TEST"
      },
      "foo-test-3FKGH6": {
        "ordertxid": "00TEST",
        "postxid": "00TEST"
      }
    },
    "count": 2346
  }
}

Expected output with object names within the body, this is a repeating element so needs to be dynamically added:

{
  "trades": [
    {
      "ordertxid": "00TEST",
      "postxid": "00TEST",
      "ID": "foo-test-UCI7BS"
    },
    {
      "ordertxid": "00TEST",
      "postxid": "00TEST",
      "ID": "foo-test-3FKGH6"
    }
  ]
}

I have already been successful in getting the array but I need the object name to be included within the body

{
  "trades": [
    {
      "ordertxid": "00TEST",
      "postxid": "00TEST"
    },
    {
      "ordertxid": "00TEST",
      "postxid": "00TEST"
    }
  ]
}

Spec:

[
  {
    "operation": "shift",
    "spec": {
      "result": {
        "trades": {
          "*": "trades"
        }
      }
    }
  }
]
Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
DavidMc
  • 19
  • 4

1 Answers1

0

What you need is $ symbol in order to grab key names of the each objects, and get rid of them within the second shift transformation such as

[
  {
    "operation": "shift",
    "spec": {
      "result": {
        "trades": {
          "*": {
            "@": "&2[0].&",
            "$": "&2[0].&.ID"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": "trades"
        }
      }
    }
  }
]

enter image description here

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55