2

I need a JSON path to return all the fields while inputting the JSON array. It is like converting array JSON to flat JSON file.

Array JSON input:

{
    "field1": "ALNT12345",
    "field2": "ALNT345346",
    "field3": "2015353423",
    "field4": "2332124343",
    "arrayfield1": [
        {
            "arr1": "625347",
            "arr2": "rere"
        },
        {
            "arr1": "634441",
            "arr2": "sdfsd"
        }
    ]
}

The above array json must be converted to 2 records as shown below. So, i am trying to achieve this using json path.

Required Output:

[
  {
    "field1": "ALNT12345",
    "field2": "ALNT345346",
    "field3": "2015353423",
    "field4": "2332124343",
    "arr1": "625347",
    "arr2": "rere"
  },
  {
    "field1": "ALNT12345",
    "field2": "ALNT345346",
    "field3": "2015353423",
    "field4": "2332124343",
    "arr1": "634441",
    "arr2": "sdfsd"
  }
]

What I tried is in JSON path,

$.arrayfield1.*

But it returning only array fields. If JSON path is not the way to achieve this, can you please suggest javascript code to return all the fields.

thanks.

4 Answers 4

1

You might want to try this:

var arr = {
    "field1": "ALNT12345",
    "field2": "ALNT345346",
    "field3": "2015353423",
    "field4": "2332124343",
    "arrayfield1": [
        {
            "arr1": "625347",
            "arr2": "rere"
        },
        {
            "arr1": "634441",
            "arr2": "sdfsd"
        }
    ],
    "arrayfield2": [
        {
            "arr1": "625347",
            "arr2": "rere"
        },
        {
            "arr1": "634441",
            "arr2": "sdfsd"
        }
    ]
};

console.log(conversion(arr));

function conversion(arr) {
    var oneLevel = {};
    var multiLevel = [];
    for(var key in arr) {
        var item = arr[key];
        if(typeof item != "object") {
            oneLevel[key] = item;
        } else {
            multiLevel = multiLevel.concat(item);
        }
    }

    for(var i in multiLevel) {
        for(var j in oneLevel) {
            multiLevel[i][j] = oneLevel[j];
        }
    }

    return multiLevel;
}

Below is the fiddle link for testing

https://fiddle.jshell.net/06zfn55L/2/

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

Comments

0

try something like:

fiddle here

var ob = [];
data.arrayfield1.forEach(function(item) { 
    var keys = Object.keys(data);
 var e = {}; 
    keys.forEach(function(key){
        if(Object.prototype.toString.call(data[key]) != '[object Array]')
         {
           e[key] = data[key];
         }
        var subkeys = Object.keys(item)
        subkeys.forEach(function(_subkeys){
              e[_subkeys] = item[_subkeys];
        });
    });
    ob.push(e);
});

Comments

0

Try utilizing Array.prototype.map() , for in loop , Array.prototype.forEach()

var data = {
    "field1": "ALNT12345",
    "field2": "ALNT345346",
    "field3": "2015353423",
    "field4": "2332124343",
    "arrayfield1": [
        {
            "arr1": "625347",
            "arr2": "rere"
        },
        {
            "arr1": "634441",
            "arr2": "sdfsd"
        }
    ]
};

var res = data.arrayfield1.map(function(val, key) {
 var obj = {}; 
 for (var i in data) {
  if (!Array.isArray(data[i])) {
   obj[i] = data[i]
  }
 }
  var names = Object.keys(val);
  names.forEach(function(value, index) {
    obj[names[index]] = val[value]
  })
 return obj
});
 
console.log(JSON.stringify(res, null, 4))

Comments

0

Try this :-

var x = {
    "field1": "ALNT12345",
    "field2": "ALNT345346",
    "field3": "2015353423",
    "field4": "2332124343",
    "arrayfield1": [
        {
            "arr1": "625347",
            "arr2": "rere"
        },
        {
            "arr1": "634441",
            "arr2": "sdfsd"
        }
    ]
};



var arr = [];

if(x.hasOwnProperty('arrayfield1')) {
    x.arrayfield1.forEach(function(obj1){
        var obj = JSON.parse(JSON.stringify(x));
        delete obj.arrayfield1;
        for(key in obj1) {
            obj[key] = obj1[key];
        }
        arr.push(obj);
    });
}   

console.log(arr);

Comments

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.