Convert string with dot notation to JSON

javascript dot notation to string
convert json to dot notation online
dot notation to json java
javascript convert string to object reference
convert string to path javascript
javascript dot string
javascript convert string to nested object
javascript eval dot notation

Given a string as dot notation, how would I create an object from that string (checking for already existing properties): eg

var obj = {};
stringToObj('a.b', 'value1', obj);
stringToObj('a.b.c', 'value2', obj);

would produce

{
   "a": {
    "b": {
        "_x": "value1",
        "c": {
            "_x": "value2"
        }
    }
    }
 }

I've looked at this question and this one but neither seems to be sufficient for what Im doing.

Any thoughts?


You can take advantage of references:

function stringToObj(path,value,obj) {
    var parts = path.split("."), part;
    while(part = parts.shift()) {
        if( typeof obj[part] != "object") obj[part] = {};
        obj = obj[part]; // update "pointer"
    }
    obj["_x"] = value;
}

Convert JavaScript string in dot notation into an , If one needs to convert dot-notation strings like "x.a.b.c" into references, it could var obj = {a:{b:{c:1, d:2}}}; console.log("obj is:", JSON.stringify(obj)); var� There are many use cases for converting JSON to a dot notation. For example, my current company used Hue to query our log data. Our logs are stored in JSON, and Hue queries expect a dot notation.


For those of you who are looking for solution without the _x in the object try this code. A slight modification of the above code (which is brilliant)

stringToObj = function(path,value,obj) {
  var parts = path.split("."), part;
  var last = parts.pop();
  while(part = parts.shift()) {
   if( typeof obj[part] != "object") obj[part] = {};
   obj = obj[part]; // update "pointer"
  }
 obj[last] = value;
}

As bonus the above code will work if you want to update parts of an existing object :)

 var obj = {a:{b:3}};
 stringToObj("a.b",10,obj);
 console.log(obj); //result : {a:{b:10}}

Convert Javascript string in dot notation into an , pulled from SO http://stackoverflow.com/questions/6393943/convert-javascript- string-in-dot-notation-into-an-object-reference. // NOTE: Array.reduce() may not be� The return value for a dot-notation query is always a string (data type VARCHAR2(4000)) representing JSON data. The content of the string depends on the targeted JSON data, as follows: If a single JSON value is targeted, then that value is the string content, whether it is a JSON scalar, object, or array.


To expand on both @ilikeopensource and @Niet the Dark Absol answers, I needed to add the support for arrays (multiple instances of the same path) with the possibility for nested objects. My case was more of a hybrid of dot notation and json strings.

function stringToObj(path, value, obj) {
    var objValue = value;
    try {
        objValue = JSON.parse(value);
    } catch (e) { } //eat the error, must not be json so carry on... Hack to do a valid JSON check

    var parts = path.split("."), part;
    var last = parts.pop();
    while (part = parts.shift()) {
        if (typeof obj[part] != "object")
            obj[part] = {};
        obj = obj[part];
    }
    if (obj.hasOwnProperty(last) && obj[last] && obj[last].constructor === Array) {
        obj[last].push(objValue);
    }
    else if (obj.hasOwnProperty(last) && obj[last]) {
        var objArray = [];
        objArray.push(obj[last])
        objArray.push(objValue);
        obj[last] = objArray;
    }
    else {
        obj[last] = objValue;
    }
}

Thanks guys for the help!

rhalff/dot-object: Transform json objects using dot notation, To convert manually per string use: var dot = require('dot-object'); var tgt = { val: ' test' }; dot.str('this.is.my.string', 'value', tgt); console.log(tgt); { "val": "test", "this":� Transform json objects using dot notation. Contribute to rhalff/dot-object development by creating an account on GitHub.


Parse dot notation to nested object - JSFiddle, $('body').append("<pre>" + JSON.stringify(Object.expand(obj), undefined, 4) + "</ pre>");. CSS Tidy. xxxxxxxxxx. 1. 1. . Result 101px. Console (beta). The DOM provides random access to data in a JSON payload. The JSON elements that compose the payload can be accessed via the JsonElement type. The JsonElement type provides array and object enumerators along with APIs to convert JSON text to common .NET types. JsonDocument exposes a RootElement property.


Convert JavaScript string in dot notation into an object reference, how can I convert the string to dot notation so I can go JSON is a string that represents an object in JavaScript-compatible notation. An object� The ConvertTo-Json cmdlet converts any .NET object to a string in JavaScript Object Notation (JSON) format. The properties are converted to field names, the field values are converted to property values, and the methods are removed. You can then use the ConvertFrom-Json cmdlet to convert a JSON-formatted string to a JSON object, which is easily managed in PowerShell. Many web sites use JSON


Property accessors, There are two ways to access properties: dot notation and bracket notation. ' value' , since both foo and bar are converted to the same string. Convert a string representation of dot-notion key into json format object. It is particularly useful for converting req.query with dot notation as the key into json. e . g .