-2

I'm learning JS and I have a challenge on my work but I have been spending a lot of time without get the correct structure on this JSON. So this is my problem:

The main JSON comes like:

{
    "Museum_Name":"Anchorage Museum",
    "Street_Address":"625 C St.",
    "City":"Anchorage",
    "State":"AK",
    "Full_State_Name":"Arkansas",
    "Zip_Code":99577,
    "Phone":"907-929-9201",
    "Website":"link",
    "Hours":"October to April:  Sat 10 a.m. to 6 p.m.; May to September: Sat 9 a.m. to  6 p.m.",
    "Provided_Description":"The Anchorage Museum is the largest museum in Alaska and one of the top 10 most visited attractions in the state. The museum’s mission is to share and connect Alaska with the world through art, history and science. Located in the heart of downtown Anchorage, the museum’s collection and exhibitions tell the story of Alaska’s past, present and future. Visitor favorites include Alaska Native artifacts, Alaska art and history galleries, a planetarium and a hands-on science center for families.",
    "Special_Note":"(No Sundays)",
    "Latitude_Longitude":"61.215906,-149.884839"
}

Repeating the same structure with a lot of cities, states and museums, I used the following linq code:

var query = $.Enumerable.From(data)
    .GroupBy(
        "{ Id: $.Full_State_Name}",
        "{ City_Name: $.City, Museum_Name: $.Museum_Name, link: $.Website}",
        "{ State: $.Id, Cities: $$.ToArray() }",
        "String($.Id) + $.City_Name + $.Website "
    )
    .ToJSON();

To get this new JSON:

[
    {
        "State": "California",
        "Cities": [
            {
                "City_Name": "Long Beach",
                "Museum_Name": "Museum of Latin American Art",
                "link": "link"
            },
            {
                "City_Name": "Los Angeles",
                "Museum_Name": "Skirball Cultural Center",
                "link": "link"
            },
            {
                "City_Name": "Los Angeles",
                "Museum_Name": "\nAutry National Center",
                "link": "link"
            }
        ]
    }
]

And i need following structure but I don't know how to get it:

{
    "State": "California",
    "City": [
        {
            "City_Name": "Long Beach",
            "Museums": [
                {
                    "Museum_Name": "Museum of Latin American Art",
                    "link": "link"
                }
            ]
        },
        {
            "City_Name": "Los Angeles",
            "Museums": [
                {
                    "Museum_Name": "Skirball Cultural Center",
                    "link": "link"
                },
                {
                    "Museum_Name": "Autry National Center",
                    "link": "link"
                }
            ]
        }
    ]
}

I really appreciate any help

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
rod.ova
  • 3
  • 2
  • Have you tried anything? – Etheryte Oct 19 '15 at 20:19
  • Just a note: JQuery is for HTML DOM manipulation. The solution for this only requires JavaScript. – OneCricketeer Oct 19 '15 at 20:21
  • Welcome to StackOverflow @rod.ova. Can you show us what you achieved with your Javascript code? – Danny Fardy Jhonston Bermúdez Oct 19 '15 at 20:24
  • 1
    This may help to learn the basics about objects: [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/q/11922383/218196) – Felix Kling Oct 19 '15 at 20:26
  • you could use something like taffyDB that has a groupBy command – dandavis Oct 19 '15 at 20:29
  • @DannyFardyJhonstonBermúdez I updated the info to show you what I did – rod.ova Oct 19 '15 at 20:38
  • Thanks @dandavis I'm looking right now taffyDB – rod.ova Oct 19 '15 at 20:39
  • It is not clear how you are getting all this data. You example source data shows only a single "museum" entry, yet your desired data format seems to include the concept of multiple cities in a state, each with one or more museums. The example data doesn't show anything like this. Added tag for `linq.js` which it looks like is a key piece of your code. – Mike Brant Oct 19 '15 at 20:40
  • @MikeBrant Are you sure you did? I don't see anything like that in the revision history. – Daedalus Oct 19 '15 at 20:50
  • @MikeBrant there are clearly 2 Los Angeles that would get mapped to one item with child array of 2 museums – charlietfl Oct 19 '15 at 20:50
  • Hi @MikeBrant I'm getting a big JSON with the first structure, the entire JSON repeat the same structure with different data, some States and Cities are repeating but with different "Museum_Name". Makes sense? – rod.ova Oct 19 '15 at 20:51

1 Answers1

0

I was able to get your second-to-last JSON block to your final JSON block. Note: JavaScript isn't my language of choice, so edits are encouraged. Let data be the block with multiple states and the museums are not nested.

var new_data = [];
for (var state_index = 0; state_index < data.length; state_index++) {
    var state = data[state_index];
    var name = state.State;
    var cities = [];
    var city_index_map = {};

    for (var city_index = 0; city_index < state.Cities.length; city_index++) { 
        var city = state.Cities[city_index];   
        var museum = { "Museum_Name" : city.Museum_Name, "link" : city.link }

        var city_data = {};
        // City doesn't exist, so add it
        if (!(city.City_Name in city_index_map)) {
            city_index_map[city.City_Name] = city_index;
            city_data.City_Name = city.City_Name;
            city_data.Museums = [ museum ];
            cities.push(city_data);
        } else { // City already exists, so append the data
            var tmp_city_index = city_index_map[city.City_Name];
            cities[tmp_city_index].Museums.push(museum);
        }
    }
    new_data.push( {"State" : name, "Cities" : cities} );
}   

Here is a fiddle for you - http://jsfiddle.net/g6p8pypj/1/

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245