Delphi SuperObject - is there a (recursive) search function that tells where a value can be found?












2















I'm using SuperObject to create and manipulate a simple hierarchical structure in JSON.



My goal is to transform a set of objects {"id":..., "name":..., "parent":...} into a hierarchical structure. Example:



I want to transform this



    {"id": "0001","name": "item0001", "parent":""},
{"id": "0002","name": "item0002", "parent":""},
{"id": "0003","name": "item0003", "parent":""},
{"id": "0003.1","name": "item0003.1", "parent":"0003"},
{"id": "0003.1.1","name": "item0003.1.1", "parent":"0003.1"},


into this



{
"items": [
{
"id": "0001",
"name": "item0001"
},
{
"id": "0002",
"name": "item0002"
},
{
"id": "0003",
"name": "item0003",
"items": [
{
"id": "0003.1",
"name": "item0003.1",
"items": [
{
"id": "0003.1.1",
"name": "item0003.1.1"
}
]
}
]
}
]
}


(This structure can vary, i.e. there is no fixed model. Which probably means the solution must be recursive).



I think the way to achieve this is:




  • for each object to add,


    • if there is no parent, add it to the output json, at the top;

    • if there is a parent, find where the parent is in the output json.

    • add the object to the output json under the parent.




To do this, I was looking for a way to retrieve the path of an object, like



function findpathinObject(key:string, value:string, object:iSuperObject):string


which would return the "path" of the value found.



In my example, findpathinObject("parent", "0003.1", newObject) would return 'items[2].items[0]'



Is this a good approach? Is there something that resolves my issue without making a new function?



the closest I've seen is this
SuperObject - Extract All
but I don't know if that can be changed to return the path it is looking in, or the path where it finally found the value...



Thanks










share|improve this question























  • curious about why the question was downvoted after the very detailed description of the problem and the research I did trying to get to a solution. I can't improve the question if there is no explanation for the downvote.

    – costateixeira
    Nov 14 '18 at 14:25
















2















I'm using SuperObject to create and manipulate a simple hierarchical structure in JSON.



My goal is to transform a set of objects {"id":..., "name":..., "parent":...} into a hierarchical structure. Example:



I want to transform this



    {"id": "0001","name": "item0001", "parent":""},
{"id": "0002","name": "item0002", "parent":""},
{"id": "0003","name": "item0003", "parent":""},
{"id": "0003.1","name": "item0003.1", "parent":"0003"},
{"id": "0003.1.1","name": "item0003.1.1", "parent":"0003.1"},


into this



{
"items": [
{
"id": "0001",
"name": "item0001"
},
{
"id": "0002",
"name": "item0002"
},
{
"id": "0003",
"name": "item0003",
"items": [
{
"id": "0003.1",
"name": "item0003.1",
"items": [
{
"id": "0003.1.1",
"name": "item0003.1.1"
}
]
}
]
}
]
}


(This structure can vary, i.e. there is no fixed model. Which probably means the solution must be recursive).



I think the way to achieve this is:




  • for each object to add,


    • if there is no parent, add it to the output json, at the top;

    • if there is a parent, find where the parent is in the output json.

    • add the object to the output json under the parent.




To do this, I was looking for a way to retrieve the path of an object, like



function findpathinObject(key:string, value:string, object:iSuperObject):string


which would return the "path" of the value found.



In my example, findpathinObject("parent", "0003.1", newObject) would return 'items[2].items[0]'



Is this a good approach? Is there something that resolves my issue without making a new function?



the closest I've seen is this
SuperObject - Extract All
but I don't know if that can be changed to return the path it is looking in, or the path where it finally found the value...



Thanks










share|improve this question























  • curious about why the question was downvoted after the very detailed description of the problem and the research I did trying to get to a solution. I can't improve the question if there is no explanation for the downvote.

    – costateixeira
    Nov 14 '18 at 14:25














2












2








2








I'm using SuperObject to create and manipulate a simple hierarchical structure in JSON.



My goal is to transform a set of objects {"id":..., "name":..., "parent":...} into a hierarchical structure. Example:



I want to transform this



    {"id": "0001","name": "item0001", "parent":""},
{"id": "0002","name": "item0002", "parent":""},
{"id": "0003","name": "item0003", "parent":""},
{"id": "0003.1","name": "item0003.1", "parent":"0003"},
{"id": "0003.1.1","name": "item0003.1.1", "parent":"0003.1"},


into this



{
"items": [
{
"id": "0001",
"name": "item0001"
},
{
"id": "0002",
"name": "item0002"
},
{
"id": "0003",
"name": "item0003",
"items": [
{
"id": "0003.1",
"name": "item0003.1",
"items": [
{
"id": "0003.1.1",
"name": "item0003.1.1"
}
]
}
]
}
]
}


(This structure can vary, i.e. there is no fixed model. Which probably means the solution must be recursive).



I think the way to achieve this is:




  • for each object to add,


    • if there is no parent, add it to the output json, at the top;

    • if there is a parent, find where the parent is in the output json.

    • add the object to the output json under the parent.




To do this, I was looking for a way to retrieve the path of an object, like



function findpathinObject(key:string, value:string, object:iSuperObject):string


which would return the "path" of the value found.



In my example, findpathinObject("parent", "0003.1", newObject) would return 'items[2].items[0]'



Is this a good approach? Is there something that resolves my issue without making a new function?



the closest I've seen is this
SuperObject - Extract All
but I don't know if that can be changed to return the path it is looking in, or the path where it finally found the value...



Thanks










share|improve this question














I'm using SuperObject to create and manipulate a simple hierarchical structure in JSON.



My goal is to transform a set of objects {"id":..., "name":..., "parent":...} into a hierarchical structure. Example:



I want to transform this



    {"id": "0001","name": "item0001", "parent":""},
{"id": "0002","name": "item0002", "parent":""},
{"id": "0003","name": "item0003", "parent":""},
{"id": "0003.1","name": "item0003.1", "parent":"0003"},
{"id": "0003.1.1","name": "item0003.1.1", "parent":"0003.1"},


into this



{
"items": [
{
"id": "0001",
"name": "item0001"
},
{
"id": "0002",
"name": "item0002"
},
{
"id": "0003",
"name": "item0003",
"items": [
{
"id": "0003.1",
"name": "item0003.1",
"items": [
{
"id": "0003.1.1",
"name": "item0003.1.1"
}
]
}
]
}
]
}


(This structure can vary, i.e. there is no fixed model. Which probably means the solution must be recursive).



I think the way to achieve this is:




  • for each object to add,


    • if there is no parent, add it to the output json, at the top;

    • if there is a parent, find where the parent is in the output json.

    • add the object to the output json under the parent.




To do this, I was looking for a way to retrieve the path of an object, like



function findpathinObject(key:string, value:string, object:iSuperObject):string


which would return the "path" of the value found.



In my example, findpathinObject("parent", "0003.1", newObject) would return 'items[2].items[0]'



Is this a good approach? Is there something that resolves my issue without making a new function?



the closest I've seen is this
SuperObject - Extract All
but I don't know if that can be changed to return the path it is looking in, or the path where it finally found the value...



Thanks







delphi search superobject






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 5:13









costateixeiracostateixeira

397




397













  • curious about why the question was downvoted after the very detailed description of the problem and the research I did trying to get to a solution. I can't improve the question if there is no explanation for the downvote.

    – costateixeira
    Nov 14 '18 at 14:25



















  • curious about why the question was downvoted after the very detailed description of the problem and the research I did trying to get to a solution. I can't improve the question if there is no explanation for the downvote.

    – costateixeira
    Nov 14 '18 at 14:25

















curious about why the question was downvoted after the very detailed description of the problem and the research I did trying to get to a solution. I can't improve the question if there is no explanation for the downvote.

– costateixeira
Nov 14 '18 at 14:25





curious about why the question was downvoted after the very detailed description of the problem and the research I did trying to get to a solution. I can't improve the question if there is no explanation for the downvote.

– costateixeira
Nov 14 '18 at 14:25












1 Answer
1






active

oldest

votes


















0














SuperObject is a JSON access library, not a data processing library. So there is nothing like this available in the box.



You just need to implement the extraction logic in pascal code, using SuperObject for reading the input, and creating the nested output.






share|improve this answer
























  • Thanks. I noticed some built-in functions and I was wondering if anyone had seen some function (or implemented something) that would iterate through all the elements and keep track of the path. The problem description was also to get advice on whether I was looking at this the right way. I will try to implement it.

    – costateixeira
    Nov 14 '18 at 18:40













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293565%2fdelphi-superobject-is-there-a-recursive-search-function-that-tells-where-a-v%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














SuperObject is a JSON access library, not a data processing library. So there is nothing like this available in the box.



You just need to implement the extraction logic in pascal code, using SuperObject for reading the input, and creating the nested output.






share|improve this answer
























  • Thanks. I noticed some built-in functions and I was wondering if anyone had seen some function (or implemented something) that would iterate through all the elements and keep track of the path. The problem description was also to get advice on whether I was looking at this the right way. I will try to implement it.

    – costateixeira
    Nov 14 '18 at 18:40


















0














SuperObject is a JSON access library, not a data processing library. So there is nothing like this available in the box.



You just need to implement the extraction logic in pascal code, using SuperObject for reading the input, and creating the nested output.






share|improve this answer
























  • Thanks. I noticed some built-in functions and I was wondering if anyone had seen some function (or implemented something) that would iterate through all the elements and keep track of the path. The problem description was also to get advice on whether I was looking at this the right way. I will try to implement it.

    – costateixeira
    Nov 14 '18 at 18:40
















0












0








0







SuperObject is a JSON access library, not a data processing library. So there is nothing like this available in the box.



You just need to implement the extraction logic in pascal code, using SuperObject for reading the input, and creating the nested output.






share|improve this answer













SuperObject is a JSON access library, not a data processing library. So there is nothing like this available in the box.



You just need to implement the extraction logic in pascal code, using SuperObject for reading the input, and creating the nested output.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 17:32









Arnaud BouchezArnaud Bouchez

37.3k356135




37.3k356135













  • Thanks. I noticed some built-in functions and I was wondering if anyone had seen some function (or implemented something) that would iterate through all the elements and keep track of the path. The problem description was also to get advice on whether I was looking at this the right way. I will try to implement it.

    – costateixeira
    Nov 14 '18 at 18:40





















  • Thanks. I noticed some built-in functions and I was wondering if anyone had seen some function (or implemented something) that would iterate through all the elements and keep track of the path. The problem description was also to get advice on whether I was looking at this the right way. I will try to implement it.

    – costateixeira
    Nov 14 '18 at 18:40



















Thanks. I noticed some built-in functions and I was wondering if anyone had seen some function (or implemented something) that would iterate through all the elements and keep track of the path. The problem description was also to get advice on whether I was looking at this the right way. I will try to implement it.

– costateixeira
Nov 14 '18 at 18:40







Thanks. I noticed some built-in functions and I was wondering if anyone had seen some function (or implemented something) that would iterate through all the elements and keep track of the path. The problem description was also to get advice on whether I was looking at this the right way. I will try to implement it.

– costateixeira
Nov 14 '18 at 18:40




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293565%2fdelphi-superobject-is-there-a-recursive-search-function-that-tells-where-a-v%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python