question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Q: Using transform operator to delete variable keys

See original GitHub issue

Hi,

I’m struggling to use the transform operator to delete a key without knowing its name (the string can’t be static in my use case). Is this possible? Can I use a @ context variable binding with transform operator in order to delete the matching element? Are there other alternative?

Here is my example where I’d want to delete any key/value object at any level in the hierarchy (the example is just one level) whose readOnly property is true.

image

Thanks, Fabrice

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
lepinskcommented, Nov 28, 2020

@andrew-coleman I found one edge case that I wanted to address here, in the event that someone else stumbles on this thread.

This will fail on any data structure that contains arrays within it, I believe because $each presumes it’s always operating on objects (Argument 1 of function "each" does not match function signature).

I’ve handled that case (I think) with the following modification (available to test here):

(
  $promote := function($obj) {
    $type($obj) = "array" ? (
        [$map($obj, $promote)]
    ) : (
        $each($obj, function($v,$k){
            {$k: ($v.targetKey ? $v.targetKey : $v[*] ? $promote($v) : $v)}
        }) ~> $merge()
    )
  };
  $promote($)
)

I’m using a $type conditional test to decide whether to iterate through the values using $map (in the case of an array), or proceed to $each as usual. (I’m enclosing the return from $map in square brackets to ensure that it doesn’t convert single-element arrays to objects.)

0reactions
lepinskcommented, Nov 27, 2020

@andrew-coleman Incredible — thanks so much for this. (And for the great work on JSONata!)

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to remove the key-value pairs corresponding to the given ...
In this article let us see how to remove key-value pairs corresponding to a given key in the object. Using delete operator. When...
Read more >
Transformation script variables
Multiple variables can be used to define explicit mapping relationships in a transform map script.
Read more >
7. Transforming Data - Q for Mortals
The $ operator is overloaded to parse strings into data of any type exactly as the q interpreter does. This overload is invoked...
Read more >
Shell Parameter Expansion (Bash Reference Manual) - GNU.org
Omitting the colon results in a test only for a parameter that is unset. Put another way, if the colon is included, the...
Read more >
Manipulating data - Ansible Documentation
In many cases, you need to do some complex operation with your variables, ... filters: used to change/transform data, used with the |...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found