1

From the below Json, I am trying to build an array as shown below.

"Issues": [
        {
            "Id": null,
            "Key": null,
            "Values": [
                {
                    "Key": "Display Name",
                    "Value": "Rya"
                },
                {
                    "Key": "UserName",
                    "Value": "RH"
                },
                {
                    "Key": "Count",
                    "Value": "350"
                }
            ]
        },
        {
            "Id": null,
            "Key": null,
            "Values": [
                {
                    "Key": "Display Name",
                    "Value": "Mike"
                },
                {
                    "Key": "UserName",
                    "Value": "ML"
                },
                {
                    "Key": "Count",
                    "Value": "90"
                }
            ]
        }
    ]

Desired Array:

{ "Display Name": 'Rya', "UserName" : "RH", value: 350 },
 { "Display Name": 'Mike', "UserName" : "ML", value: 90 }

Here, there can be any number of fields in the array, but not confined to "Display Name" "UserName" and "Value". so I want to build that array dynamically.

1
  • 2
    There is no such thing as a "JSON Object" – JSON is always a string. There are no exceptions. Commented Mar 22, 2017 at 18:18

2 Answers 2

1

I assume "Issues" is a property of an object you have.

You could use this ES6 code, using reduce:

const result = obj.Issues.map( o => 
    o.Values.reduce( (acc, {Key, Value}) =>
        (acc[Key] = Value, acc), {}));

const obj = {
"Issues": [
        {
            "Id": null,
            "Key": null,
            "Values": [
                {
                    "Key": "Display Name",
                    "Value": "Rya"
                },
                {
                    "Key": "UserName",
                    "Value": "RH"
                },
                {
                    "Key": "Count",
                    "Value": "350"
                }
            ]
        },
        {
            "Id": null,
            "Key": null,
            "Values": [
                {
                    "Key": "Display Name",
                    "Value": "Mike"
                },
                {
                    "Key": "UserName",
                    "Value": "ML"
                },
                {
                    "Key": "Count",
                    "Value": "90"
                }
            ]
        }
    ]
};

const result = obj.Issues.map( o => 
    o.Values.reduce ( (acc, {Key, Value}) =>
        (acc[Key] = Value, acc), {}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Or in ES5 compatible code, and without the (bit obscure) comma operator:

var result = obj.Issues.map(function (o) {
    return o.Values.reduce(function (acc, kv) {
        acc[kv.Key] = kv.Value;
        return acc;
    }, {})
});

var obj = {
"Issues": [
        {
            "Id": null,
            "Key": null,
            "Values": [
                {
                    "Key": "Display Name",
                    "Value": "Rya"
                },
                {
                    "Key": "UserName",
                    "Value": "RH"
                },
                {
                    "Key": "Count",
                    "Value": "350"
                }
            ]
        },
        {
            "Id": null,
            "Key": null,
            "Values": [
                {
                    "Key": "Display Name",
                    "Value": "Mike"
                },
                {
                    "Key": "UserName",
                    "Value": "ML"
                },
                {
                    "Key": "Count",
                    "Value": "90"
                }
            ]
        }
    ]
};

var result = obj.Issues.map(function (o) {
    return o.Values.reduce (function (acc, kv) {
        acc[kv.Key] = kv.Value;
        return acc;
    }, {})
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

7 Comments

I think ()-expressions like (expr1, expr2, ..., exprN) //=> exprN have been around since pre-es5; correct me if I'm wrong, please. Anyway, good answer ^_^
Yes, that is right, I just thought to write it in a more common (and elaborate) way as alternative.
@trincot have a similar case trying to figure out how to do. My existing array is [ { "Key": "WWC-383", "Creator": "Indra", "Summary": "Test Summary", "Created": "2016-12-19" }, { "Key": "WWC-382", "Creator": "Sukumar", "Summary": "Testing WorkDesk", "Created": "2016-12-15" } ].. The array that I need is [{ "title": "WWC-383", "start": "2016-12-19" }, { "title": "WWC-382", "start": "2016-12-15" } ] to supply to my calendar gadget.
arr.map( ({Key, Created}) => ({ title: Key, start: Created }) ), but really you should not ask new questions in comments, but press the "Ask Question" button ;-)
Thanks. Sure I will do that.
|
-1

You have to use ajax to send:

$.ajax({

    url: your url,
    data: { Display Name: 'Rya', etc },
    type: "post",
    dataType: "json",
    success: function (data) {

        // what you want
 });

And use this function ( if you are using C#) to recivie:

JsonConvert.SerializeObject();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.