3

As shown in sample input, I have array of extension in each object. When extension doesn't consists subscription-type then output should be null as shown in output or else should consist existing value. Same should be applicable for language-type. Order of subscription-type and language-type is random in nature.

I have tried with spec but its not working

  {
    "operation": "modify-define-beta",
    "spec": {
      "*": {
        "subscriptionType": {
          "url": {
            "myCoercedValue": "subscription-type",
            "myStringValue": "subscription-type"
          },
          "value": {
            "myCoercedValue": null,
            "myStringValue": null
          }
        },
        "languageType": {
          "url": {
            "myCoercedValue": "language-type",
            "myStringValue": "language-type"
          },
          "value": {
            "myCoercedValue": null,
            "myStringValue": null
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "extension": "[&1].extension",
        "subscriptionType": "[&1].extension[]",
        "languageType": "[&1].extension[]"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "extension": {
          "*": {
            "@url": {
              "myStringValue": {
                "subscription-type": {
                  "@(3,value.myStringValue)": "[&3].subscriptionType[]"
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "0": "[]"
    }
  }
]

Input:

[
  {
    "extension": []
  },
  {
    "extension": [
      {
        "url": {
          "myCoercedValue": "subscription-type",
          "myStringValue": "subscription-type"
        },
        "value": {
          "myCoercedValue": "free",
          "myStringValue": "free"
        },
        "extension": []
      }
    ]
  },
  {
    "extension": [
      {
        "url": {
          "myCoercedValue": "language-type",
          "myStringValue": "language-type"
        },
        "value": {
          "myCoercedValue": "en-us",
          "myStringValue": "en-us"
        }
      },
      {
        "url": {
          "myCoercedValue": "subscription-type",
          "myStringValue": "subscription-type"
        },
        "value": {
          "myCoercedValue": "free",
          "myStringValue": "free"
        }
      }
    ]
  }
]

Output:

[ {
  "subscriptionType" : [ null, "free", "free" ],
  "language":[null,null,"en-US"]
} ]
  • Do you want to put default value to "url" -> "myCoercedValue" object? You have to think about all this by keys, not by values. That is why your first spec - modify-define-beta - doesn't work, because in your input there is not any "subscriptionType" or "languageType" objects. – Magda Aug 08 '19 at 09:33

1 Answers1

2

Perhaps you want to get such a result? Please, try those spec and say what you want to modify.

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "extension": {
          "*": {
            "url": {
              "myCoercedValue": {
                "subscription-type": {
                  "@(3,value)": {
                    "myCoercedValue": "subscriptionType[#8]"
                  }
                },
                "language-type": {
                  "@(3,value)": {
                    "myCoercedValue": "language[#8]"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]
Magda
  • 482
  • 4
  • 9
  • Thanks But can you please explain what is the meaning of #8. it seems like if that data is not there it will put null. Am I rite ? – Dhrumil Shah Aug 13 '19 at 06:02
  • Yes, #8 means go 8 levels up the tree, and ask the first node, how many of it's children have been matched and if no more data found there it put null – Magda Aug 13 '19 at 06:52
  • Sorry for response after long time but its not working if First empty `"extension" : []` is at last position. – Dhrumil Shah Aug 31 '23 at 06:58