how to parse json string contains circular reference in javascript? - javascript

I have the following json string in javascript. This string contains a circular references. I want to parse this string in such a way that the reference will be replaced by its actual object. I use Json.Parse but it creates the json object with references. Is there any way by whihc i can achieve this ?
{
"$id": "1",
"$values": [
{
"$id": "2",
"Event": {
"$id": "3",
"Invitaions": {
"$id": "4",
"$values": [
{
"$ref": "2"
},
{
"$id": "5",
"Event": {
"$ref": "3"
},
"Id": 2,
"Name": "test2",
"Date": "24",
"EventId": 1
}
]
},
"Id": 1,
"Name": "marriage",
"Address": "abcd"
},
"Id": 1,
"Name": "test1",
"Date": "23",
"EventId": 1
},
{
"$ref": "5"
},
{
"$id": "6",
"Event": {
"$id": "7",
"Invitaions": {
"$id": "8",
"$values": [
{
"$ref": "6"
}
]
},
"Id": 2,
"Name": "birthday",
"Address": "abcd"
},
"Id": 3,
"Name": "test3",
"Date": "25",
"EventId": 2
}
]
}

This should do it:
function resolveReferences(json) {
if (typeof json === 'string')
json = JSON.parse(json);
var byid = {}, // all objects by id
refs = []; // references to objects that could not be resolved
json = (function recurse(obj, prop, parent) {
if (typeof obj !== 'object' || !obj) // a primitive value
return obj;
if ("$ref" in obj) { // a reference
var ref = obj.$ref;
if (ref in byid)
return byid[ref];
// else we have to make it lazy:
refs.push([parent, prop, ref]);
return;
} else if ("$id" in obj) {
var id = obj.$id;
delete obj.$id;
if ("$values" in obj) // an array
obj = obj.$values.map(recurse);
else // a plain object
for (var prop in obj)
obj[prop] = recurse(obj[prop], prop, obj)
byid[id] = obj;
}
return obj;
})(json); // run it!
for (var i=0; i<refs.length; i++) { // resolve previously unknown references
var ref = refs[i];
ref[0][ref[1]] = byid[refs[2]];
// Notice that this throws if you put in a reference at top-level
}
return json;
}

You should check out Douglas Crockfords JSON-js repo on github: https://github.com/douglascrockford/JSON-js
There's a cycle.js in there that helps you do exactly what you're looking for.

Look at my post here, I've found some bugs in the code above and there wasn't arrays support, check out my improved version: Resolve circular references from JSON object

Related

How do I set value of nested object to key of current object?

This array has the key to substitute with nested key of 'name'
const arr = ['status', 'user', ...] <-- This array contains key to be replaced with name
This is what my current response object is
[
{
"id": 11,
"location": "Mumbai",
"status": {
"name": "NEW"
},
"user": {
"name": "Rakesh"
}
}
]
How do I modify the above array of objects to this below
[
{
"id": 11,
"location": "Mumbai",
"status": "NEW",
"user": "Rakesh"
}
]
can try below code
const keys = ['status', 'user']
let arr = [
{
"id": 11,
"location": "Mumbai",
"status": {
"name": "NEW"
},
"user": {
"name": "Rakesh"
}
}
]
arr.map(a => keys.forEach(k => {
if(a[k] && a[k].name) a[k] = a[k].name
}));
console.log(arr);
I'd try this one:
const results = [
{
"id": 11,
"location": "Mumbai",
"status": {
"name": "NEW"
},
"user": {
"name": "Rakesh"
}
}, {
"id": 12,
"location": "Helsinki",
"status": {
"name": "NEW"
},
"user": {
"name": "Samuli"
}
}
];
const flattenObject = ([key, value]) => ((typeof value === 'object') ? {[key] : value[Object.keys(value)[0]]} : {[key]: value});
const reduceToSingleObject = (acc, b) => ({...acc, ...b});
const actualResults = results.map((result) => Object.entries(result).map(flattenObject).reduce(reduceToSingleObject));
console.log(actualResults);
Explanation:
flattenObject is a function to flatten structure of object inside object. This only takes the first prop (without knowing the name of the key). If you, for some reason, would need to flatten several key-values, then it'd need whole different kind of helper function to sort that out.
reduceToSingleObject is a function to put all the key-value pairs into a single object. This could have been done already in flattenObject function, but for the clarity, I separated it to a normal map - reduce pattern.
actualResults is the outcome where we go through all the entries of your original results.

Parsing a dynamic JSON object

I am working on parsing a JSON and searching for specific key in that JSON object. The problem is that the structure of JSON keeps changing and i cannot hard code the path, is there any better ways to parse?
or
can i get this to convert in to regular JSON key value pairs as below
{
"resourceName": "Bundle",
"id": "6d6e-81d5-5a1e2b452563",
"lastUpdated": "2069-06-21",
"url": "http://abcd.com",
.
.
.
... so on
}
I have tried using hard coded methods but that doesnt seem to work always
Here is snipped of JSON
{
"resourceName": "Bundle",
"id": "6d6e-81d5-5a1e2b452563",
"meta": {
"lastUpdated": "2069-06-21"
},
"data1": [{
"url": "http://abcd.com",
"value": {
"url": "http://abcd.com",
"value": [{
"url": "Severity",
"value": "info"
}, {
"url": "dfew",
"value": "fefdd"
}, {
"url": "fwef",
"value": "This is data blah blah"
}]
}
}]
}
You search the object recursively:
function get_lastUpdated(obj)
{
for ( var key in obj )
{
if ( key == "lastUpdated" ) return obj[key];
if ( typeof obj[key] == "object" )
{
var res = get_lastUpdated(obj[key]);
if ( res ) return res;
}
}
return null;
}
For a simple case, something like above could work.
When you call JSON.parse, you can pass in a callback function, called a reviver, which will be recursively applied to all key-value pairs. For example:
var jsonString = `{
"resourceName": "Bundle",
"id": "6d6e-81d5-5a1e2b452563",
"meta": {
"lastUpdated": "2069-06-21"
},
"data1": [{
"url": "http://abcd.com",
"value": {
"url": "http://abcd.com",
"value": [{
"url": "Severity",
"value": "info"
}, {
"url": "dfew",
"value": "fefdd"
}, {
"url": "fwef",
"value": "This is data blah blah"
}]
}
}]
}`;
var obj = {};
JSON.parse(jsonString, (key, value) => {
if (typeof value === "string" && !(key in obj)) {
obj[key] = value;
}
});
console.log(obj);
If have already parsed the object, you can use a similar strategy with JSON.stringify and the replacer callback:
var data = {
"resourceName": "Bundle",
"id": "6d6e-81d5-5a1e2b452563",
"meta": {
"lastUpdated": "2069-06-21"
},
"data1": [{
"url": "http://abcd.com",
"value": {
"url": "http://abcd.com",
"value": [{
"url": "Severity",
"value": "info"
}, {
"url": "dfew",
"value": "fefdd"
}, {
"url": "fwef",
"value": "This is data blah blah"
}]
}
}]
};
var obj = {};
JSON.stringify(data, (key, value) => {
if (typeof value === "string" && !(key in obj)) {
obj[key] = value;
}
return value;
});
console.log(obj);

How to iterate nested json object?

I have a nested JSON Object and i want to iterate that.
JSON Response
{
"specifications": {
"IP6": {
"name": "Devices",
"productSubType": "Device",
"productSpecificationType": "Phones"
}
},
"offers": {
"US-PRE-IPHONE-CASE": {
"path": "productDetails/IP6",
"familyData": {
"0": "Missing Family Level data Should be here"
},
"facets": [],
"type": [],
"offers": {
"US-PRE-HG-PH-IP6": {
"hashDigest": "cf23df2207d99a74fbe169e3eba035e633b65d94",
"offerName": "offerNameString",
"productName": "iPhone 6 Case Mate Naked Tough Case - Clear",
"productOfferings": {
"ratings": "4.5",
"noOfReviews": "2010"
},
"offerStatus": {},
"displayPriority": "100200",
"descriptions": {
"shortDescription": "Iphone Decription ",
"longDescription": "longDescriptionStri6 descriptionng",
"alternativeDescription": "alternativeDescriptionString",
"reprsentativeDescription": ""
},
"specifications": [
"someSpecificationId1"
],
"brand": "Apple",
"productType": "Device",
"productSubType": "Phone",
"offerType": "",
"offerSubType": "",
"compatibility": {},
"classification": [],
"images": {
"thumbanail": {
"imagePath": "http://s.tmocache.com/images/png/products/accessories/SUPM43270/SUPM43270-small.png"
}
},
"equipmentCharacteristics": {},
"offerVariants": {},
"type": "hard-good",
"offers": [],
"family": "IP6",
"pricePoints": {
"withServicePrice16GBNEW": {
"displayPriority": "1001",
"pricingMessage": "device price with service activation",
"price": "34.99",
"discounts": {}
}
},
"dynamicPricingData": {},
"inventoryData": {
"SKUGOLD16GBN": {
"availibility": "Pre-order now!",
"availableTimeline": ""
}
}
}
}
}
}
}
Now as you see there are nested JSON objects in this and I want the value of
productName
shortDescription
imagePath
availibility
What I have tried is
function change(){
var acc = response; //response is JSON Object mentioned above
var accArray = [];
var accArray1 = [];
for (var obj in acc.specifications){
accArray.push(obj);
}
alert(accArray[0]);
for (var obj in accArray[0].offers){
accArray1.push(obj);
}
alert(accArray1[0]);
}
I am able to get the first object the first alert output is
IP6
but when I am trying to iterarte the IP6 object in same way the output is
undefined
I want to fetch all the 4 values as I mentioned above and then put them in an array.
As Grundy pointed out in his comment, obj in your code is the key of properties/items in specifications object. That means 'obj' is just a string.
To get reference to the object, change your code as below:
for(var obj in acc.specifications){
accArray.push(acc.specifications[obj]);
}
For better readability change obj to key
You can use for..in loop and recursion.
function find(obj, fieldName){
if(Array.isArray(obj)){
for(var i=0, len=obj.length;i<len;i++){
var nested = find(obj[i],fieldName);
if(nested.isFind) return nested;
}
}else{
if(typeof obj !== "object") return {isFind:false};
for(var i in obj){
if(i === fieldName) return {isFind:true, value:obj[i]};
var nested = find(obj[i],fieldName);
if(nested.isFind) return nested;
}
}
return {isFind:false};
}
this function return object with field isFind for case when available value can be null or undefined
var obj = {
"specifications": {
"IP6": {
"name": "Devices",
"productSubType": "Device",
"productSpecificationType": "Phones"
}
},
"offers": {
"US-PRE-IPHONE-CASE": {
"path": "productDetails/IP6",
"familyData": {
"0": "Missing Family Level data Should be here"
},
"facets": [],
"type": [],
"offers": {
"US-PRE-HG-PH-IP6": {
"hashDigest": "cf23df2207d99a74fbe169e3eba035e633b65d94",
"offerName": "offerNameString",
"productName": "iPhone 6 Case Mate Naked Tough Case - Clear",
"productOfferings": {
"ratings": "4.5",
"noOfReviews": "2010"
},
"offerStatus": {},
"displayPriority": "100200",
"descriptions": {
"shortDescription": "Iphone Decription ",
"longDescription": "longDescriptionStri6 descriptionng",
"alternativeDescription": "alternativeDescriptionString",
"reprsentativeDescription": ""
},
"specifications": [
"someSpecificationId1"
],
"brand": "Apple",
"productType": "Device",
"productSubType": "Phone",
"offerType": "",
"offerSubType": "",
"compatibility": {},
"classification": [],
"images": {
"thumbanail": {
"imagePath": "http://s.tmocache.com/images/png/products/accessories/SUPM43270/SUPM43270-small.png"
}
},
"equipmentCharacteristics": {},
"offerVariants": {},
"type": "hard-good",
"offers": [],
"family": "IP6",
"pricePoints": {
"withServicePrice16GBNEW": {
"displayPriority": "1001",
"pricingMessage": "device price with service activation",
"price": "34.99",
"discounts": {}
}
},
"dynamicPricingData": {},
"inventoryData": {
"SKUGOLD16GBN": {
"availibility": "Pre-order now!",
"availableTimeline": ""
}
}
}
}
}
}
}
function find(obj, fieldName){
if(Array.isArray(obj)){
for(var i=0, len=obj.length;i<len;i++){
var nested = find(obj[i],fieldName);
if(nested.isFind) return nested;
}
}else{
if(typeof obj !== "object") return {isFind:false};
for(var i in obj){
if(i === fieldName) return {isFind:true, value:obj[i]};
var nested = find(obj[i],fieldName);
if(nested.isFind) return nested;
}
}
return {isFind:false};
}
var result = ['productName','shortDescription','imagePath','availibility'].map(function(el){ return find(obj,el).value});
document.getElementById('r').innerHTML = JSON.stringify(result,null,2);
<pre id='r'></pre>
Your json code is a complex data binding structure. It same like c# complex data binding. So you need to call the obj by through it call name.
for eg:
var data = {"ex":{"a":{"a1":"a1","a2":"a2"},"b":{"b1":"b1","b2":"b2"}}}
so data is a class and it includes "ex" object
data returns =>Object {ex: Object}
if you need to access "a" or "b" object means , you need to access through the"ex" object.
for eg:
data.ex.a => Object {a1: "a1", a2: "a2"}
in your code
for(var obj in acc.specifications){
accArray.push(obj);
}
obj only push 1st element of acc.sppectification object.
So please try this.
foreach(var obj acc.specification){
arr1.push(acc.specification[obj])
}
foreach (var obj acc.offers){
arr2.push(acc.offers[obj])
}

How to check that object with same ID (or any other attribute ) exists in array of objects?

I have following array:
var array = [
{
"milestoneTemplate": {
"id": "1",
"name": "TEST1"
},
"id": "1",
"date": "1416680824",
"type": "ETA",
"note": "Note",
"color": "66FF33"
},
{
"milestoneTemplate": {
"id": "2",
"name": "Test 2"
},
"id": "2",
"date": "1416680824",
"type": "ATA",
"note": "Note 22",
"color": "66FF00"
}
];
And now i would like to check in forEach loop that object (which is passed in param of the function) is existing in array by his ID.
In case that not = do push into existing array.
arrayOfResults.forEach(function(entry) {
if(entry != existingInArrayByHisId) {
array.push(entry);
}
});
Thanks for any advice
You could create a helper function thats checks if an array contains an item with a matching property value, something like this:
function checkForMatch(array, propertyToMatch, valueToMatch){
for(var i = 0; i < array.length; i++){
if(array[i][propertyToMatch] == valueToMatch)
return true;
}
return false;
}
which you can then use like so:
arrayOfResults.forEach(function (entry) {
if (!checkForMatch(array, "id", entry.id)) {
array.push(entry);
}
});

Is there any way to get an object from a json object that has an unpredictable key/name?

JSON object:
{
"users": {
"1403365934870556481": {
"userId": "1403365934863466416",
"id": "1403365934870556481",
"relationType": "CHILD",
"nickname": "Nameson",
"active": "1",
"created": "2014-06-21 16:52:14",
"relationDetails": {
"userId": "1403365934863466416",
"title": "UNSELECTED",
},
"hasFinanceAccount": true
},
"1406296959993232380": {
"userId": "1406296959978437904",
"id": "1406296959993232380",
"relationType": "CHILD",
"nickname": "Izzy",
"active": "1",
"created": "2014-07-25 15:02:39",
"relationDetails": {
"userId": "1406296959978437904",
"title": "UNSELECTED",
},
"hasFinanceAccount": true
}
}
}
Scheme of the object:
--> somenumber -> obj
obj->users
--> somenumber -> obj
The only thing I can think is parse that object as string and replace the number with something else
any suggestion?
My Solution is:
var tmp = JSON.stringify( currentPageData.getChildren );
tmp = t.replace(/[\d]+\"\:\{\"userId/g, 'child":{"userId');
myobj = JSON.parse(tmp);
var myobj = {
3: "three"
}
console.log(myobj[3]) // prints "three"
var obj = { 1234567890: 'test', asdf: 'okay' };
var keys = Object.keys(obj);
console.log(obj[keys[0]]); // 'test'
console.log(obj[keys[1]]); // 'okay'
update
var obj = JSON.parse(json);
var values = Object.keys(obj.users).map(function(key){ return obj.users[key]});
values.forEach(function(value){
if (value["userId"]){
console.log(value["nickname"]);
}
});
DEMO

Categories