1

I have a JSON generated by Papa Parse from a CSV file that look like this:

{ object, object, object, .... , object }

I need to modify it so it looks like this:

{ "publication" : 
    {
      "id" : 1,
      "name" : "name1"
    },
  "publication" :
    {
      "id" : 2,
      "name" : "name2"
    },
  "publication" :
    {
      "id" : 3,
      "name" : "name3"
    }
}

Where the "id" and "name" are properties within each object.

In other words, I need a JSON that say "publication" and each "publication" occurrence have nested under it the properties names and values of one of the objects above.

How could I achieve this with Javascript?

Thanks

7
  • 1
    Did you try anything? Commented Apr 1, 2015 at 22:21
  • 6
    Described structure is not possible. You can only have one property with the key Publication. Commented Apr 1, 2015 at 22:22
  • You can't have several things with the same key. Commented Apr 1, 2015 at 22:22
  • It wouldn't make sense to have things with the same key since there would be ambiguity ... what would data.publication return? name1? name2? Commented Apr 1, 2015 at 22:23
  • 1
    +1 to @dfsq. You are probably looking for an array that looks something like {publications : [ { "id": 1, ...}, ... ] } Commented Apr 1, 2015 at 22:26

3 Answers 3

2

you cannot put many keys with same name...

a solution it's put in array

{ 
    "publication" : [
      {
        "id" : 1,
        "name" : "name1"
      },
      {
        "id" : 2,
        "name" : "name2"
      },
      {
        "id" : 3,
        "name" : "name3"
      }]
}
Sign up to request clarification or add additional context in comments.

Comments

1

Is this not an array of publications?

   { publications: [{"id":1, "name":"name1"},
         {"id":2, "name":"name2"}.....]}

Comments

1

If I understand it right, you probably mean that you have objects like this:

{"publication" : { "id" : 1, "name" : "name1" }
{"publication" : { "id" : 2, "name" : "name2" }
{"publication" : { "id" : 3, "name" : "name3" }

What you can do in order to have:

{ "publication" : [ 
    {
        "id" : 1, 
        "name" : "name1"
    }, 
    {
        "id" : 1, 
        "name" : "name2"
    }, 
    {
        "id" : 3, 
        "name" : "name3"
    } ] 
}

is:

var json = [{"publication" : { "id" : 1, "name" : "name1" }},
                {"publication" : { "id" : 2, "name" : "name2" }},
                {"publication" : { "id" : 3, "name" : "name3" }}];

var newJson = {"publication" : []};

    var i = 0;
    for (var item in json) {
        var key = json[item];
        var obj = {"id" : key["publication"]["id"], "name" : key["publication"]["name"]};
        newJson["publication"][i] = obj;
        i++;
    }

You can check this if you want to print "pretty":

How can I pretty-print JSON using JavaScript?

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.