Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9240519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:12:12+00:00 2026-06-18T08:12:12+00:00

I’m creating an index file in JSON, which I’m using as a sort-of-database index

  • 0

I’m creating an index file in JSON, which I’m using as a sort-of-database index for a javascript application I’m working on.

My index will look like this:

{
    "_id": "acomplex_indices.json",
    "indexAB": {
        "title": {
            "Shawshank Redemption": [
                "0"
            ],
            "Godfather": [
                "1"
            ],
            "Godfather 2": [
                "2"
            ],
            "Pulp Fiction": [
                "3"
            ],
            "The Good, The Bad and The Ugly": [
                "4"
            ],
            "12 Angry Men": [
                "5"
            ],
            "The Dark Knight": [
                "6"
            ],
            "Schindlers List": [
                "7"
            ],
            "Lord of the Rings - Return of the King": [
                "8"
            ],
            "Fight Club": [
                "9"
            ],
            "Star Wars Episode V": [
                "10"
            ],
            "Lord Of the Rings - Fellowship of the Ring": [
                "11"
            ],
            "One flew over the Cuckoo's Nest": [
                "12"
            ],
            "Inception": [
                "13"
            ],
            "Godfellas": [
                "14"
            ]
        },
        "year": {
            "1994": [
                "0",
                "3"
            ],
            "1972": [
                "1"
            ],
            "1974": [
                "2"
            ],
            "1966": [
                "4"
            ],
            "1957": [
                "5"
            ],
            "2008": [
                "6"
            ],
            "1993": [
                "7"
            ],
            "2003": [
                "8"
            ],
            "1999": [
                "9"
            ],
            "1980": [
                "10"
            ],
            "2001": [
                "11"
            ],
            "1975": [
                "12"
            ],
            "2010": [
                "13"
            ],
            "1990": [
                "14"
            ]
        }
    }
}

So for every keyword (like Pulp Fiction), I’m storing the matching document-id(s).

My problem is with integers/numbers/non-string data, like the release year in the above example. This is stored as a string, while I had hoped it would be stored as a number.

I’m creating the index entries like this:

// indices = the current index file
// doc = the document to update the index with
// priv.indices = all indices defined for this application instance
// priv.indices.fields = index fields e.g. "year", "director", "title"
// priv.indices.name = name of this index

priv.updateIndices = function (indices, doc) {
var i, j, index, value, label, key, l = priv.indices.length;

// loop all indices to add document
for (i = 0; i < l; i += 1) {
  index = {};
  index.reference = priv.indices[i];
  index.reference_size = index.reference.fields.length;
  index.current = indices[index.reference.name];

  for (j = 0; j < index.reference_size; j += 1) {
    label = index.reference.fields[j];    // like "year"
    value = doc[label];                   // like 1985

    // if document has a label field (e.g. doc.year = 1985)
    if (value !== undefined) {

      // check if the index file already contains an entry for 1985
      index.current_size = priv.getObjectSize(index.current[label]);
      
      if (index.current_size > 0) {
        // check if the document id is already in the index
        // in case the data is updated (e.g. change 1982 to 1985)
        key = priv.searchIndexByValue(
          index.current[label],
          doc._id,
          "key"
        );
        if (!!key) {
          delete index.current[label][key];
        }
      }
      // create a new array if 1985 is not in the index yet
      if (index.current[label][value] === undefined) {
        index.current[label][value] = [];
      }
      // add the document id to an existing entry
      index.current[label][value].push(doc._id);
    }
  }
}
return indices;
};

This works fine, except that fields I want to store as non-strings (integers, numbers or datetime), like the year in the above example end up as strings in my index.

Question:
Is it at all possible to store "non-string" types in a JSON document? If so, can I also store the key of a key/value pair as a "non-string" element.

If not, would I have to add a parameter to my index definitions declaring the type of each key in order to modify the key-string when I run into it or is there a better way to do it?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:12:13+00:00Added an answer on June 18, 2026 at 8:12 am

    Is it at all possible to store "non-string" types in a JSON document?

    Yes. The value of a property can be a string, number, boolean, object, array or null (undefined is a notable exception – it’s a native JavaScript type but it’s not a valid JSON value).

    Can I also store the key of a key/value pair as a "non-string" element?

    No. The key name must always be a string. However, that doesn’t mean you can’t parse that string into some other JavaScript type. For example, if you have a string but need a number, you can use the parseInt function, or the unary + operator.

    See the JSON grammar for more detail.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to render a haml file in a javascript response like so:
I am using JSon response to parse title,date content and thumbnail images and place
I used javascript for loading a picture on my website depending on which small
I would like to run a str_replace or preg_replace which looks for certain words
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a reasonable size flat file database of text documents mostly saved in
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am using jsonparser to parse data and images obtained from json response. When

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.