Iceape
Iceape

Reputation: 211

How can I output an object as json without the property names?

I have a WebAPI 2 project with a few APIs that return data in Json format. These Json files are then loaded into graphs that a separate front-end development agency has created. These graphs accept Json files in this format:

"colors": {
    "Europe": "red",
    "North-America": "blue",
    "South-America": "green",
    "Asia": "purple",
    "Other": "grey"
}

My WebAPI project outputs data as Json, but it includes the property names like this:

"colors": [
{
  "Title": "Europe",
  "ColorName": "red"
},
{
  "Title": "North-America",
  "ColorName": "blue"
},
{
  "Title": "South-America",
  "ColorName": "green"
},
{
  "Title": "Asia",
  "ColorName": "purple"
},
{
  "Title": "Other",
  "ColorName": "grey"
}

]

Is there a standard way to output the data without property names?

I am using a standard installation of WebAPI 2 with all of its defaults, which includes references to Newtonsoft.Json.

Upvotes: 1

Views: 1260

Answers (3)

caner
caner

Reputation: 721

when you get the api data, change like this...

 $(document).ready(function () {

    //your api json data
    var rootObject = {
        "colors": [
{
"Title": "Europe",
"ColorName": "red"
},
{
"Title": "North-America",
"ColorName": "blue"
},
{
"Title": "South-America",
"ColorName": "green"
},
{
"Title": "Asia",
"ColorName": "purple"
},
{
"Title": "Other",
"ColorName": "grey"
}]
    }
    var jsonArr = [];
    for (var i = 0; i < rootObject.colors.length; i++) {
        var temp={};
        temp[rootObject.colors[i].Title] = rootObject.colors[i].ColorName;

        jsonArr.push(temp);
    }

    var stringJson = JSON.stringify(jsonArr);

    alert(stringJson );
    });

Upvotes: 0

peco
peco

Reputation: 4000

If you are using json.net, one way would be to define a custom JsonConverter:

public class Color
{
    public string Title { get; set; }
    public string ColorName { get; set; }
}

public class ColorsConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(Color[]);
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        var colors = (Color[]) value;

        var temp = colors.ToDictionary(x => x.Title, x => x.ColorName);

        serializer.Serialize(writer, temp);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

And then use it:

var colors = new[]
{
    new Color {Title = "Europe", ColorName = "red"},
    new Color {Title = "North-America", ColorName = "blue"},
    new Color {Title = "South-America", ColorName = "green"},
    new Color {Title = "Asia", ColorName = "purple"},
    new Color {Title = "Other", ColorName = "grey"}
};

var json = JsonConvert.SerializeObject(new {colors}, new ColorsConverter());

This will produce json like this:

{
  "colors": {
    "Europe": "red",
    "North-America": "blue",
    "South-America": "green",
    "Asia": "purple",
    "Other": "grey"
  }
}

Upvotes: 2

IbraHim M. Nada
IbraHim M. Nada

Reputation: 660

If u Decode it it will be the Same both of them can be decoded as json object its a mean to and end hope that helps

Upvotes: 0

Related Questions