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
}
}
12
  • 1
    @Passerby: PHP is mentioned nowhere in the question. Commented Jan 15, 2013 at 5: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 Commented Jan 15, 2013 at 5:09
  • Sorry. So this JSON data is available by appending ?format=json-pretty on pages of the Squarespace website platform. Example: 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. Commented Jan 15, 2013 at 5: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? Commented Jan 15, 2013 at 5:14
  • 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? Commented Jan 15, 2013 at 5:24

2 Answers 2

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]} )
  }
}
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)

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.