0

Is there any plugin or function that converts multi-dimensional JSON like this:

{
    "hello" : {
        "foo" : "bar",
        "arr" : ["a", "b", "c"]
    },
    "another": {
        "go" : {
            "very" : {                
                "deep" : 1                
            }
        }
    }
}

To array in this format

[
    {"key" : "another[go][very][deep]", "value" : "1"),
    {"key" : "hello[arr][]", "value" :a"),
    {"key" : "hello[arr][]", "value" :b"),
    {"key" : "hello[arr][]", "value" :c"),
    {"key" : "hello[foo]", "value" :bar")
]

Or do I need to write it at my own? Forgive me if I am wrong but when jQuery makes ajax call input JSON has to be converted to data in format above?

I am trying to create function/plugin that creates form with hidden fields to be sent into <iframe>

So basically function like this but that allows multi-dimensional params

Community
  • 1
  • 1
Peter
  • 16,453
  • 8
  • 51
  • 77
  • May be a duplicate of http://stackoverflow.com/questions/19098797/fastest-way-to-flatten-un-flatten-nested-json-objects – Jaay Oct 02 '15 at 08:30
  • Shouldn't `"hello[arr][]" : "a"` be `"hello[arr][1]" : "a"` and so, otherwise the values will be overridden because they all are having same key – Tushar Oct 02 '15 at 08:31
  • @Tushar I copied it from firebug console, that's how data is sent to my server and PHP parses it correctly – Peter Oct 02 '15 at 08:33
  • @Jaay no, different output – Peter Oct 02 '15 at 08:34
  • @Peter the output you have there is not valid JSON; it doesn't have commas after each mapping. Can you print the result that is received server-side? – Brandon Oct 02 '15 at 08:35
  • i don't think you need to change data, sending json will do. Have you tried sending data as it is? – valepu Oct 02 '15 at 08:35
  • @Intredasting oh you guys were right while it was valid post data, it was invalid json. I edited my answer and I tried to write my function (see answer) – Peter Oct 02 '15 at 08:44
  • @valepu please read my question, i tried to send data to iframe thru form – Peter Oct 02 '15 at 08:54
  • Please don't change your requirements of your questions making existing questions target the old requirements. – PeeHaa Oct 02 '15 at 09:34
  • @PeeHaa Should I answer new question? Sorry at the I didnt realize that multiple `"hello[arr][]"` is ok POST data but incorrect JSON – Peter Oct 02 '15 at 10:21
  • Perhaps ping the answerer below his answer asking him nicely to review it if possible. – PeeHaa Oct 02 '15 at 10:22

1 Answers1

0

Ok I made my custom function:

$.postirify = function(obj) {
    var tmp = $.param(obj).split("&");
    console.log(tmp);
    var result = [];
    $.each(tmp, function(i, v) {
        var kv = v.split("=");        
        result.push({
            key: decodeURIComponent(kv[0]),
            value: decodeURIComponent(kv[1])
        });
    });
    return result;
}

Demo: http://jsfiddle.net/9wL9zz8L/

And in the result I can create my form with hidden data that can be submitted to <iframe>

$.fn.hiddenForm = function(data) {        
    var $form = $(this);
    $form.html("");
    var p = $.postirify(data);
    $.each(p, function(i, kv){
        var $input = $('<input/>');
        $input.attr("type", "hidden");
        $input.attr("name", kv.key);
        $input.val(kv.value);
        $form.append($input);
    });
    return $form;
};

$('#my-form').hiddenForm($.postirify(my_data)).submit();
Peter
  • 16,453
  • 8
  • 51
  • 77