0

I have JSON :

[
  {
    "Position": "ProjectManager",
    "Salary": 2000
  },
  {
    "Position": "BusinessAnalyst",
    "Salary": 2001
  },
  {
    "Position": "TechnicalLead",
    "Salary": 2002
  },
  {
    "Position": "SeniorSoftwareEngineer",
    "Salary": 2003
  },
  {
    "Position": "SoftwareEngineer",
    "Salary": 2004
  },
  {
    "Position": "JuniorSoftwareEngineer",
    "Salary": 2005
  },
  {
    "Position": "UIUXEngineer",
    "Salary": 2006
  },
  {
    "Position": "QALead",
    "Salary": 2007
  },
  {
    "Position": "SeniorQAEngineer",
    "Salary": 2008
  },
  {
    "Position": "QAEngineer",
    "Salary": 2009
  },
  {
    "Position": "JuniorQAEngineer",
    "Salary": 2010
  },
  {
    "Position": "SeniorAutomationEngineer",
    "Salary": 2011
  },
  {
    "Position": "AutomationEngineer",
    "Salary": 2012
  },
  {
    "Position": "JuniorAutomationEngineer",
    "Salary": 2013
  }
]

But I need to convert it into this example :

{
  "ProjectManager": "2000",
    "BusinessAnalyst": "2001",
    "TechnicalLead": "2002",
    "SeniorSoftwareEngineer": "2003",
    "SoftwareEngineer": "2004",
    "JuniorSoftwareEngineer": "2005",
    "UIUXEngineer": "2006",
    "QALead": "2007",
    "SeniorQAEngineer": "2008",
    "QAEngineer": "2009",
    "JuniorQAEngineer": "2010",
    "SeniorAutomationEngineer": "2011",
    "AutomationEngineer": "2012",
    "JuniorAutomationEngineer": "2013"
}

As you see in 2nd example i have just values and no properties. How can i do this? (Currentrly my idea is to parse Json as string and remove all tokens that match "Position :" or "Salary :")

2 Answers 2

2

You dont need to doo so much a stuff. I can metioned a method.

let your json one is probjson and result asresjson

var arr=new [];
var res=new { arr }
    for(var i in probjson.length){

var position=probjson[i].Position;
var salary=probjson[i].Salary;

var v = new { position=salary  }
res.arr.add(V);

}

this not a complete one

plz try to get a idea. thankz

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

Comments

0

One approach would be iterating each object in the array and populate a dictionary:

// Using JSON.NET you can deserialize the JSON array as an enumerable 
// of dynamically-typed objects (i.e. your array)
IEnumerable<dynamic> employees = JsonConvert
                                   .DeserializeObject<IEnumerable<dynamic>>
                                   (
                                       jsonText
                                   );

Dictionary<string, object> values = new Dictionary<string, object>();

foreach(dynamic employee in employees) 
{
    values.Add((string)employee.Position, (object)employee.Salary);
}

// .NET dictionaries are deserialized into JSON objects!
string convertedJson = JsonConvert.SerializeObject(values);

Check a working sample on .NET Fiddle!

Update

And if you want a one-liner LINQ extension methods-based solution, what about:

string convertedJson = JsonConvert.SerializeObject
(
    JsonConvert.DeserializeObject<IEnumerable<dynamic>>(jsonText)
        .ToDictionary(employee => employee.Position, employee => employee.Salary)
);

1 Comment

@user3770925 No problem. Check my update, you might be able to simplify even more the solution! ;)

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.