0

So I have a script that generates 4 variables: NAME, SMA, BBLOWER, and BBUPPER with each iteration of a loop, and adds them as a new row to an existing array. So my array is of the form

[[NAME,SMA,BBLOWER,BBUPPER],[NAME,SMA,BBLOWER,BBUPPER],[NAME, SMA,BBLOWER,BBUPPER],etc]

What I'd like to do is rather than add them to an array, I'd like to turn them into a JSON file with a specific structure:

{
    "NAME" : [{
            "SMA" : <value>,
            "BBLOWER" : <value>,
            "BBUPPER" : <value>,
        }],
    "NAME" : [{
            "SMA" : <value>,
            "BBLOWER" : <value>,
            "BBUPPER" : <value>,
        }], etc
    ]
}

Now that I've typed that out I'm not even sure that correct structure. Anyway, I basically want to be able to take the resultant json file and easily extract the SMA value (for example and using the name "VGS") by simply entering something like:

smaValue = json.VGS.SMA

or the BBUPPER of market "VAS" like this:

bbUpperValuer = json.VAS.BBUPPER

Is this approach even practical? There other ways I could do the same thing using the array, but doing it the JSON way just seems more clean and "proper".

1
  • Are you asking how to convert your array into JSON? Commented Aug 25, 2017 at 23:55

2 Answers 2

1

It's easy to get confused between a JavaScript object literal (a definition in your source code of an actual JavaScript object), the JavaScript object itself, and JSON (a format for representing data as a string of characters, inspired by JavaScript's syntax).

I mention this because your question seems to touch on several different aims:

  • changing the structure of a variable from a flat array to a more multi-dimensional object
  • storing an object, of whatever structure, to a file, serialized as a JSON string, and later reading it back and recreating the object
  • accessing properties of a multi-dimensional object, whether created by normal JavaScript manipulation, or by parsing a JSON string

For the first part, you just need to change the way your script works from writing to an array to setting properties of objects. You don't need to declare the structure up front. Without seeing your code, it's hard to be more specific, but it will likely combine direct property access to fixed field names like item.UPPER = foo(); with dynamic access to properties not known in advance like results[getName()] = item;

The second part is easy - but possibly also irrelevant to the task at hand. Just use dataToStore = JSON.stringify(something) to turn an object into a string in JSON format, and something = JSON.parse(dataLoadedFromStore) to turn it back.

The third part you've answered yourself - the example syntax you give is exactly what you can do with the object created at the first step. Again, to labour the point, it has nothing at all to do with JSON, it's just how you access objects in JavaScript.

Sign up to request clarification or add additional context in comments.

4 Comments

Well holy balls thank you for the detailed explanation! I think I'm most of the way to what I need. After defining my variables above I simply added a line bollingerData[i] = {Market:name,SMA:SMA,BBTOP:bbTop,BBBOTTOM:bbBottom}; which gives me something very close to what I am after: '[ { Market: 'VAS',SMA: 29019.44,BBTOP: 29019.44,BBBOTTOM: 29019.44 }]' The trick now is modifying that so that it's { "VAS" : [{ "SMA" : <value>,"BBLOWER" : <value>,"BBUPPER" : <value>}]}
@Mattaus I'm not sure why you want the single-element array in there, but using the name as the key is as simple as initialising the empty object at the beginning with bollingerData={} then writing bollingerData[name] instead of bollingerData[i].
thanks for the tip. In my head it'll make the data easier to work with later on as rather than searching through the array for the required name, I can just address it directly. I'm not sure it will function exactly the way I envisage but only one way to see :)
Works exactly as I desired. Thanks!
0

JSON is the way to go. I am guessing that's the reason why mongoDB uses JSON while storing data in its structure.

You can just do

{
    "VGS" : {
            "SMA" : <value>,
            "BBLOWER" : <value>,
            "BBUPPER" : <value>,
        },
    "VAS" : {
            "SMA" : <value>,
            "BBLOWER" : <value>,
            "BBUPPER" : <value>,
        }, etc
}

Call them like this

smaValue = json["VGS"]["SMA"];
bbUpperValuer = json["VAS"]["BBUPPER"];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.