1

Hey guys i am trying to change the Json data to my required Json and store it into a new Json. My Json data is

var dataSet = {"ContractNumber":["17102367","17054217","17835641","17597810","17131176","17592158","17601558","17457472","17843672","17555883"],
"ContractType":["CP4","USV","EAT","UNI","UPS","UPS","UPS","UPS","UPS","UPS","123"]}

required Json in

var dataSet =[
["17102367", "CP4"],
["17054217", "UNI"],
["17054666", "UN3"],
["17054217", "U23"],
["17102367","CP4"],
["17054217","USV"],
["17835641","EAT"],
["17597810","UNI"],
["17131176","UPS"],
["17592158","UPS"],
["17601558","UPS"],
["17457472","UPS"],
["17843672","UPS"],
["17555883","UPS"],
["","123"]
]
amit kaushik
  • 79
  • 2
  • 9
  • none of this is JSON – Jaromanda X Jan 12 '16 at 00:57
  • i checked in http://jsoneditoronline.org/ and it got passed as a json – amit kaushik Jan 12 '16 at 01:06
  • That's great you're trying to code, you can write it in your Facebook status, but StackOverflow is for asking for help with a code, which is not working for some reason. Please don't create questions with stories what you're currently working on here, please. – David Ferenczy Rogožan Jan 12 '16 at 01:09
  • @JaromandaX please refer to http://stackoverflow.com/questions/383692/what-is-json-and-why-would-i-use-it – Sachin Jan 12 '16 at 01:36
  • @Sachin - yes. and Javascript Object Notation is defined clearly as `a text format that is completely language independent` ... do you get that `dataSet` is **not a string** ... therefore it **can not be JSON** – Jaromanda X Jan 12 '16 at 01:57
  • hmm you might be right.. can you please post the same comment on this link http://stackoverflow.com/questions/tagged/json . This contains 14050 questions on JSON. All these stupid people think same. You should enlighten them. :) – Sachin Jan 12 '16 at 02:01

1 Answers1

4

use array map function

var dataSet = {"ContractNumber":["17102367","17054217","17835641","17597810","17131176","17592158","17601558","17457472","17843672","17555883"],
"ContractType":["CP4","USV","EAT","UNI","UPS","UPS","UPS","UPS","UPS","UPS","123"]}

dataSet = dataSet.ContractType.map(function(ct, index) {
    return [dataSet.ContractNumber[index] || '', ct];
});

The last value will be

['', '123']

because

[, '123']

is not valid javascript

edit: the [, '123'] actually is valid javascript, my bad

Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
  • @ jaromanda can yor please help me in this also [{"excludeDate":"21/01/2016","contractType":"CP6","contractNumber":"17102367"},{"excludeDate":"21/01/2016","contractType":"UC1","contractNumber":"17555883"},{"excludeDate":"21/01/2016","contractType":"U04","contractNumber":"17835641"},{"excludeDate":"21/01/2016","contractType":"CP6","contractNumber":"17102367"},{"excludeDate":"21/01/2016","contractType":"UC1","contractNumber":"17555883"},{"excludeDate":"21/01/2016","contractType":"U04","contractNumber":"17835641"}] – amit kaushik Jan 20 '16 at 06:21
  • wants to convert `var dataSet=[{"excludeDate":"21/01/2016","contractType":"CP6","contractNumber":"17102367"},{‌​"excludeDate":"21/01/2016","contractType":"UC1","contractNumber":"17555883"},{"ex‌​cludeDate":"21/01/2016","contractType":"U04","contractNumber":"17835641"},{"exclu‌​deDate":"21/01/2016","contractType":"CP6","contractNumber":"17102367"},{"excludeD‌​ate":"21/01/2016","contractType":"UC1","contractNumber":"17555883"},{"excludeDate‌​":"21/01/2016","contractType":"U04","contractNumber":"17835641"}]` – amit kaushik Jan 20 '16 at 08:34
  • to this format `var dataSet = [ ["1710‌​2367", "CP6","21/01/2016"], ["17555‌​883", "UC1","21/01/2016"], ["178356‌​41", "U04","21/01/2016"], .......]` – amit kaushik Jan 20 '16 at 08:43