skip to Main Content

I have input that looks like this:

{
  "INS-2000_loop": [
    {
      "HD-2300_loop": [
        {
          "COB-2320_loop": [
            {
              "COB_01": "11",
              "COB_02": "12",
              "COB_03": "13"
            },
            {
              "COB_01": "21",
              "COB_03": "23"
            },
            {
              "COB_01": "31",
              "COB_02": "32"
            },
            {
              "COB_02": "42",
              "COB_03": "43"
            }
          ]
        }
      ]
    }
  ]
}

Currently my spec looks like this:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "HD-2300_loop": {
            "*": {
              "COB-2320_loop": {
                "*": {
                  "COB_01": "L2320.[&1].x",
                  "COB_02": "L2320.[&1].y",
                  "COB_03": "L2320.[&1].z"
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "L2320": [
        { "x": null, "y": null, "z": null }
      ]
    }
  }
]

which gives this result:

{
  "L2320": [
    {
      "x": "11",
      "y": "12",
      "z": "13"
    },
    {
      "x": "21",
      "z": "23"
    },
    {
      "x": "31",
      "y": "32"
    },
    {
      "y": "42",
      "z": "43"
    }
  ]
}

I would like the result to have null defaults like this:

{
  "L2320": [
    {
      "x": "11",
      "y": "12",
      "z": "13"
    },
    {
      "x": "21",
      "y": null,
      "z": "23"
    },
    {
      "x": "31",
      "y": "32",
      "z": null
    },
    {
      "x": null,
      "y": "42",
      "z": "43"
    }
  ]
}

There may be any number of elements in the input array (though likely no more than 5 or 6). Is there a way for me to specify default values for the complex element attributes?

2

Answers


  1. Hi spec will help you resolve your query :

    First spec is as it is, in 2nd spec use modify-default-beta instead of default operation.

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "HD-2300_loop": {
                "*": {
                  "COB-2320_loop": {
                    "*": {
                      "COB_01": "L2320.[&1].x",
                      "COB_02": "L2320.[&1].y",
                      "COB_03": "L2320.[&1].z"
                    }
                  }
                }
              }
            }
          }
        }
      },
      {
        "operation": "modify-default-beta",
        "spec": {
          "*": {
            "*": {
              "x": null,
              "y": null,
              "z": null
            }
          }
        }
      },
      {
        "operation": "sort"
      }
    ]
    
    Login or Signup to reply.
    • You rather should use a modify transformation in which avooid
      nesting elements within square brackets
    • no need to repeat the common expressions, use * instead
    • no need to rewrite the key-value pairs, use | instead(similar to an OR operator), for the ones with common values (right hand side expressions)

    such as

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*": {
                "*": {
                  "*": {
                    "*": {
                      "*1": "L2320[&1].x",
                      "*2": "L2320[&1].y",
                      "*3": "L2320[&1].z"
                    }
                  }
                }
              }
            }
          }
        }
      },
      {
        "operation": "modify-default-beta",
        "spec": {
          "*": {
            "*": { "x|y|z": null }
          }
        }
      },
      {//order by the keys
        "operation": "sort"
      }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search