Quantcast
Channel: Active questions tagged jq - Stack Overflow
Viewing all articles
Browse latest Browse all 657

Transform a tree-like json so that single "item" in a list is moved up (from leaf), empty items deleted - using jq

$
0
0

I have a json that is like a tree of files or folders like this:

{"item": [        {"name": "objects","description": "root f","item": [                {"name": "external","item": [                        {"name": "buuu","keep1": {}                        },                        {"name": "biii","keep1": {}                        }                    ],"description": "Whatever comment."                },                {"name": "afolder","item": [                        {"name": "methods","item": [                                {"name": "blaaa","keep1": {}                                },                                {"name": "empty","description": "Whatever2.","item": []                                },                                {"name": "partner1","item": [                                        {"name": "operand","item": [                                                {"name": "whetever4","beep1": { },"beep2": []                                                }                                            ]                                        }                                    ]                                },                                {"name": "empty2","description": "Whatever3."                                },                                {"name": "partner2","item": [                                        {"name": "operandx","beep2": []                                        },                                        {"name": "operand2","item": [                                                {"name": "whatever3","beep1": { },"beep2": []                                                }                                            ]                                        }                                    ]                                }                            ]                        }                    ]                }            ]        }    ]}

The tree is formed by .item[] (optional) arrays and .name (optionally .description) fields. If there are more fields then the item is not deletable!

So there are items like

objects/   --> with "root f" comment  external --> is an item that could be deleted because has no extra fields   external/buu and external/bii  --> Are non-deletable items, do not move them up to "objects" folder! As there are two items under "exernal"! You only move up single items in a folder!objects/afolderobjects/afolder/methods  empty and empty2 are to be deleted items (leaf, and only name, description or item fields are there, and the items is practically empty.objects/afolder/methods/partner1  operand  --> is a folder to delete as you will move the "operand/whetever4" under objects/afolder/methods/partner1, and this "operand" will be empty!objects/afolder/methods/partner2 operand2 --> must be deleted after the single leaf "whatever3" is moved under objects/afolder/methods/partner2 - next to "operandx" ideally

Therefore the jq filter must produce the reorganized json while

  • You cannot refer to fields like beep1, beep2, keep1, ... in the jq expression, any number of extra fields can be there in any object
  • You should only refer to the fields: item, name, description
  • keep the order of the fields the same
  • if you add the leaf item to the parent "item" array try to add it to the same location where the directory was present (in the example, whatever3 to the location of the deleted operand2)
  • You do not move leafs if there is more than one item in the same item array (you only move up leaf if it is a single item in the folder).

So the output must be like:

{"item": [        {"name": "objects","description": "root f","item": [                {"name": "external","item": [                        {"name": "buuu","keep1": {}                        },                        {"name": "biii","keep1": {}                        }                    ],"description": "Whatever comment."                },                {"name": "afolder","item": [                        {"name": "methods","item": [                                {"name": "blaaa","keep1": {}                                },                                {"name": "partner1","item": [                                        {"name": "whetever4","beep1": { },"beep2": []                                        }                                    ]                                },                                {"name": "partner2","item": [                                        {"name": "operandx","beep2": []                                        },                                        {"name": "whatever3","beep1": { },"beep2": []                                        }                                    ]                                }                            ]                        }                    ]                }            ]        }    ]}

Viewing all articles
Browse latest Browse all 657

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>