1

i have two kinds of input the jsons

case 1:

{
  "MainObject": {
    "MainObjectDetails": {
      "SubDetails": {
        "ObjectProperty": "1"
      }
    }
  }
}

case 2:

{
  "MainObject": {
    "MainObjectDetails": {
      "SubDetails": {
        "ObjectProperty": [
          "1",
          "2"
        ]
      }
    }
  }
}

i want the resultant json to be an integer type array for both cases. How can it be done within one spec? expected result:

Case 1:

{
  "MainObject": {
    "MainObjectDetails": {
      "SubDetails": {
        "ObjectProperty": [
          1
        ]
      }
    }
  }
}

Case 2:

{
  "MainObject": {
    "MainObjectDetails": {
      "SubDetails": {
        "ObjectProperty": [
          1,
          2
        ]
      }
    }
  }
}
0

1 Answer 1

3

your problem can be solved by using two simple beta operations: =toList() and =toInteger().

The input should look like this:

{
  "MainObject": {
    "MainObjectDetails": {
      "SubDetails": {
        "ObjectProperty": [
          "1",
          "2"
        ]
      }
    }
  }
}

And the spec is this:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "MainObject": {
        "MainObjectDetails": {
          "SubDetails": {
            "ObjectProperty": "=toList"
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "MainObject": {
        "MainObjectDetails": {
          "SubDetails": {
            "ObjectProperty": "=toInteger"
          }
        }
      }
    }
  }
]

The first operation transforms the JSON in cases where the input looks like this: "ObjectProperty": "1"
It converts it to: "ObjectProperty": [1]
If the input doesn't match this case, it remains unchanged.

Then the second spec casts every value of the array to an Integer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.