Add json object key as value

38 Views Asked by At

I have a JSON:

{
  "error" : {
    "code" : "validation_failed",
    "message" : "Validation failed",
    "fields" : {
      "date_end" : {
        "code" : "min_value",
        "message" : "Value is less than minimum allowed"
      }
    }
  }
}

date_end can be date_start, name, geo. I need to extract message, I did this:

[
  {
    "operation": "shift",
    "spec": {
      "error": {
        "fields": {
          "date_end|date_start|name|geo": {
            "message": "message"
          }
        }
      }
    }
  }
]

But also I need this json object name inside message. So I expect to get this result:

{
  "message": "date_end|Value is less than minimum allowed"
}

Before message I need to add this json object name. How can I do this?

1

There are 1 best solutions below

0
Barbaros Özhan On BEST ANSWER

You can use the following transformation specs :

[
  { // convert message attribute to an array with two components 
    "operation": "shift",
    "spec": {
      "error": {
        "*": "&1.&",
        "fields": {
          "*": {
            "*": "&3.&2.&1.&",
            "$|message": "&3.&2.&1.message"
          }
        }
      }
    }
  },
  { // concatenate those components of the array
    "operation": "modify-overwrite-beta",
    "spec": {
      "error": {
        "fields": {
          "*": {
            "message": "=join('|',@(1,&))"
          }
        }
      }
    }
  }
]