sort an array within a json array with jolt

Viewed 186

I have an array that is used in an application to show some files that need to be ordered.

I wonder if it is possible to shake order the most recent "value" of the "Creation date".

we have not achieved it with the array inside an array

This is the entry:

[
  {
    "campos": [
      {
        "nombre_campo": "ID",
        "valor": "1635942768793"
      },
      {
        "nombre_campo": "Fecha creación",
        "valor": "2021-01-04 09:32:48"
      }
    ]
  },
  {
    "campos": [
      {
        "nombre_campo": "ID",
        "valor": "1632417779050"
      },
      {
        "nombre_campo": "Fecha creación",
        "valor": "2021-09-23 14:22:59"
      }
    ]
  }
]

I would:

[
  {
    "campos": [
      {
        "nombre_campo": "ID",
        "valor": "1632417779050"
      },
      {
        "nombre_campo": "Fecha creación",
        "valor": "2021-09-23 14:22:59"
      }
    ]
  },
  {
    "campos": [
      {
        "nombre_campo": "ID",
        "valor": "1635942768793"
      },
      {
        "nombre_campo": "Fecha creación",
        "valor": "2021-01-04 09:32:48"
      }
    ]
  }
]

Is it possible?

Thank you

1 Answers

We can use sort spec after a manipulation which replaces keys named campos with ID values through use of campos[0].valor( eg. &[0].valor - "the valor values within the first indexes" ) within the first shift spec, since there's no descreasing option for the sort spec, but we have the opportunity to get ID values which are descreasing while Fecha creación(eg. Creation date ) values are increasing.

So, consider applying the following specs

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "@(1,&[0].valor)"
      }
    }
  },
  {
    "operation": "sort"
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&.campos"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

enter image description here

Related