Mattaus
Mattaus

Reputation: 41

Manual JSON string creation

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".

Upvotes: 0

Views: 163

Answers (2)

IMSoP
IMSoP

Reputation: 97898

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.

Upvotes: 1

C.Unbay
C.Unbay

Reputation: 2826

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"];

Upvotes: 0

Related Questions