0

How can I wrap all of the data here in an array? This is auto generated from a website system and the app I'm feeding it into requires the data to be in an array. There are multiple sets of data in the JSON, this is just 2 of about 5 or 6.

collection: {
id: "5096f729e4b02d37bef658f2",
enabled: true,
starred: false,
type: 10,
ordering: 3,
title: "About",
navigationTitle: "About",
urlId: "about",
itemCount: 0,
updatedOn: 1347025745523,
publicCommentCount: 0,
folder: false,
dropdown: false,
tags: [ ],
categories: [ ],
homepage: true,
typeName: "page",
synchronizing: false,
typeLabel: "page",
fullUrl: "/"
},

websiteSettings: {
id: "5096f728e4b02d37bef658e0",
websiteId: "5096f728e4b02d37bef658df",
type: "Business",
subject: "Personal",
country: "US",
state: "NY",
markdownMode: false,
simpleLikingEnabled: true,
commerceEnabled: false,
defaultPostFormat: "%y/%m/%d/%t",
commentLikesAllowed: true,
commentAnonAllowed: true,
commentThreaded: true,
commentApprovalRequired: false,
commentAvatarsOn: true,
commentSortType: 2,
commentFlagThreshold: 0,
commentFlagsAllowed: true,
commentEnableByDefault: true,
commentDisableAfterDaysDefault: 0,
disqusShortname: "username",
homepageTitleFormat: "%s - This is a test",
collectionTitleFormat: "%c — %s - This is a test",
itemTitleFormat: "%i — %s - This is a test",
commentsEnabled: true,
allowSquarespacePromotion: false,
storeSettings: {
storeTitle: "Test",
returnPolicy: null,
termsOfService: null,
privacyPolicy: null,
stockLevelAlertLimit: 5,
useLightCart: false,
stripeConnected: false,
storeState: 3
}
}
Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
jasonbarone
  • 172
  • 3
  • 17
  • 1
    @Passerby: PHP is mentioned nowhere in the question. – PleaseStand Jan 15 '13 at 05:07
  • We need more info to answer question. What are you trying to do. I mean if you literally just want to wrap it in an array just add `[` and `]` to the beginning and end -- you appear already to have valid json objects – Jonah Jan 15 '13 at 05:09
  • Sorry. So this JSON data is available by appending ?format=json-pretty on pages of the Squarespace website platform. Example: http://squarespace.com/?format=json-pretty I need to use Javascript or Jquery in my app to put the data in arrays because the app only recognizes the data coming through in arrays. – jasonbarone Jan 15 '13 at 05:10
  • So the data is being given as object properties, whose keys are "collection", "website", "items" etc. Do you want an array consisting of just the values of those keys? – Jonah Jan 15 '13 at 05:14
  • Yes, this is just how Squarespace built their system. I can't change how it's outputted. So for instance the objects in "items" are already in arrays (http://blog.squarespace.com/?format=json-pretty), and my app recognizes those just fine. The data in "collection" and "website" is not in arrays and cannot be recognized. So I'm trying to use js or jquery to process this JSON after it's polled by my app in order to wrap the data in arrays. Hope that makes sense! – jasonbarone Jan 15 '13 at 05:17
  • I guess I would need all of the data in "collection", "website" and "websiteSettings" since "items" is already in an array. – jasonbarone Jan 15 '13 at 05:20
  • @PleaseStand Ops, sorry, I must confused this with other question. – Passerby Jan 15 '13 at 05:22
  • 1
    The problem is that it's not array data. It's data with keys and values. You could of course create an array consisting of just the values, but then how would your app know what each value meant? You'd have to essentially hardcode the knowledge of what each array position meant? Or do you want an array of objects, where each object contained a key and value? – Jonah Jan 15 '13 at 05:24
  • @jasonbarone, I think the reason your question makes no sense at all, is that you seem to be confusing arrays and objects. Can you clarify what you are trying to do? – Brad Jan 15 '13 at 05:26
  • In this instance, the app (and I) don't need to know where the data came from. I just need those keys/values in one large array. Edit: forgive me I don't understand this stuff. The bottom line is when the app polls blog.squarespace.com/?format=json-pretty, it returns a list of JSON data for "items", but nothing else because the other data (collection, website, and websiteSettings) is not in an array. – jasonbarone Jan 15 '13 at 05:27
  • @jasonbarone, but arrays don't contain keys and values, they contain just values. Do you want an array of objects, with each object containing a key and value? And you are sure you want to collapse down all of the categories? – Jonah Jan 15 '13 at 05:30
  • Correct, one large array with just objects would do. I don't need to know what was in collection, website, websiteSettings, or items. – jasonbarone Jan 15 '13 at 05:33

2 Answers2

1

Okay so assume you have the JSON returned from the API in a variable called rawJson. That is easy enough for you to do with jquery, for example with getJSON. Now you can you acheive what you want with this code:

var rawJson = // get this yourself, pretend I'm inserting the JSON literal from the url you linked to above in your comments
arrayYouWant = [];

for (category in rawJson) {
  for (key in rawJson[category]) {
    arrayYouWant.push( {key: rawJson[category][key]} )
  }
}
Jonah
  • 15,806
  • 22
  • 87
  • 161
0

You can also flat those two objects and convert them into an array, see the snippet below.

var rawJSON = {
  collection: {
    id: "5096f729e4b02d37bef658f2",
    enabled: true,
    starred: false,
    type: 10,
    ordering: 3,
    title: "About",
    navigationTitle: "About",
    urlId: "about",
    itemCount: 0,
    updatedOn: 1347025745523,
    publicCommentCount: 0,
    folder: false,
    dropdown: false,
    tags: [],
    categories: [],
    homepage: true,
    typeName: "page",
    synchronizing: false,
    typeLabel: "page",
    fullUrl: "/"
  },

  websiteSettings: {
    id: "5096f728e4b02d37bef658e0",
    websiteId: "5096f728e4b02d37bef658df",
    type: "Business",
    subject: "Personal",
    country: "US",
    state: "NY",
    markdownMode: false,
    simpleLikingEnabled: true,
    commerceEnabled: false,
    defaultPostFormat: "%y/%m/%d/%t",
    commentLikesAllowed: true,
    commentAnonAllowed: true,
    commentThreaded: true,
    commentApprovalRequired: false,
    commentAvatarsOn: true,
    commentSortType: 2,
    commentFlagThreshold: 0,
    commentFlagsAllowed: true,
    commentEnableByDefault: true,
    commentDisableAfterDaysDefault: 0,
    disqusShortname: "username",
    homepageTitleFormat: "%s - This is a test",
    collectionTitleFormat: "%c — %s - This is a test",
    itemTitleFormat: "%i — %s - This is a test",
    commentsEnabled: true,
    allowSquarespacePromotion: false,
    storeSettings: {
      storeTitle: "Test",
      returnPolicy: null,
      termsOfService: null,
      privacyPolicy: null,
      stockLevelAlertLimit: 5,
      useLightCart: false,
      stripeConnected: false,
      storeState: 3
    }
  }
}

var myObject = Object.assign({}, rawJSON.collection); // merging objects aka extend
myObject = Object.assign(myObject, rawJSON.websiteSettings);

// {a: 1, b: 2} => [['a', 1], ['b', 2]]
var objAsAnArray=Object.keys(myObject).map((k)=>[k, JSON.stringify(myObject[k])])

console.log(objAsAnArray)
test30
  • 3,496
  • 34
  • 26