Nodejs jsonArray parsing - json

I have a json object i want to get data from it!
here is my json object
"[androidVersionName=2.3.3, androidVersionId=10, androidId=fa0bef4b5a48eacb, mobileModel=sdk, mobileManufacturer=unknown, mobileId=GRI34, mobileProduct=sdk, applicationName=com.example.socketclient, applicationVersionName=1.0, applicationVersionCode=1, applicationState=INACTIVE, screenWidth=480, screenHeight=480, screenDensity=240, screenDensityName=hdpi, atdPackages=com.atd.panberes(1)]"
and here is my code :
var json = JSON.parse(data);
var androidVersionName = data.rowsets['androidVersionName'].row;
console.log(androidVersionName);
and i get this error :
Cannot read property 'androidVersionName' of undefined
how can i parse data from this jsonObject?

A valid JSON based on your object:
{
"androidVersionName":"2.3.3",
"androidVersionId":10,
"androidId":"fa0bef4b5a48eacb",
"mobileModel":"sdk",
"mobileManufacturer":"unknown",
"mobileId":"GRI34",
"mobileProduct":"sdk",
"applicationName":"com.example.socketclient",
"applicationVersionName":1.0,
"applicationVersionCode":1,
"applicationState":"INACTIVE",
"screenWidth":480,
"screenHeight":480,
"screenDensity":240,
"screenDensityName":"hdpi",
"atdPackages":"com.atd.panberes(1)"
}

You can't parse it to object. But you can transform it to JS object.
var data = "[androidVersionName=2.3.3, androidVersionId=10, androidId=fa0bef4b5a48eacb, mobileModel=sdk, mobileManufacturer=unknown, mobileId=GRI34, mobileProduct=sdk, applicationName=com.example.socketclient, applicationVersionName=1.0, applicationVersionCode=1, applicationState=INACTIVE, screenWidth=480, screenHeight=480, screenDensity=240, screenDensityName=hdpi, atdPackages=com.atd.panberes(1)]";
var result = {};
data.replace(/(\w+)=(\w+)/g, function(_, left, right) { result[left] = right; })
console.log(result);

Related

How do I access the information inside the response body of a postman test Get call

Below is a JSON format from the response body
{
"properties":{
"name":"Jake",
"id":123,
"HashData":[
{
"Major":"CS",
"code":234
}
]
}
}
I tried using:
var x = pm.response.json().properties;
console.log(x.HashData); // it returned HashData is [object object]
console.log(x.HashData.code); // it returned undefined
How else can I see or access the data?
And how do you use this in patch as in if you want to change code from 234 to 567?
You need to use the pm object.
Try console.log(x.HashData[0].code);
Your HasData is an array;
pm.test (
"Response HashData has a code.",
function()
{
var data = pm.response.json();
pm.expect(data.properties.HashData[0]).to.have.property('code');
}
);
This if for object but for arrays you need to use the key.
pm.test (
"Response has name property.",
function()
{
var data = pm.response.json();
pm.expect(data.properties).to.have.property('name');
}
);
More test documentation here : https://learning.postman.com/docs/postman/scripts/postman-sandbox-api-reference/
To change the value you would do something like :
data.properties.HashData[0].code = 567

Can't iterate through JSON object

I'm pulling a JSON request from the politifact API:
request('http://politifact.com/api/v/2/statement/?format=[JSON]&order_by=-ruling_date&limit=1')
.then(({
data
}) => {
newArticles = extractListingsFromJSON(data);
and parsing it with a function that the JSON is passed to
function extractListingsFromJSON(json) {
var jsonObject = json.objects
Outputs entire objects array
var headline = jsonObject[0].facebook_headline
console.log("headline:\n" + headline)
Outputs headline from Objects[0]
This works as intended. However, when I try to iterate through the objects array like so:
for (var attr in jsonObject) {
console.log(attr+": "+jsonObject.facebook_headline);
}
Outputs "0: undefined"
I also tried:
console.log(attr+": "+jsonObject[facebook_headline];
Outputs nothing
As you mentioned yourself jsonObject is an array.
json.objects.forEach(function (i) {
console.log(i.facebook_headline)
})
You still need the attr key to iterate through the jsonObject. Try doing attr+" :"+jsonObject[attr].facebook_headline
instead.

String array to jSON array in Javascript?

I have a string file like this
["ORM.mp4","bla.bla","blaa.blaa"]
an any body tell me how can I convert it to array of jSON object.
I have tried various methods like json.stringify() then json.parse()etc.
None of them worked.
Any help would be highly appreciated!
Use eval https://www.w3schools.com/jsref/jsref_eval.asp
a = eval('["ORM.mp4","bla.bla","blaa.blaa"]')
console.log(a)
var test = '["ORM.mp4","bla.bla","blaa.blaa"]';
var data = JSON.parse(test);
console.log(data );
You can use JSON.parse() for parsing json.
var obj = ["ORM.mp4","bla.bla","blaa.blaa"].reduce(function(acc, cur, i) {
acc[i] = cur;
return acc;
}, {});

Angular js Complex Json Objects

I'm newbie to Angular Js.I would like to Create Complex Json Object for my Xml settings Like
string Jsonobject=" {'Email': { 'UserName': 'a','Password': 'a'}}";
So far I tried
$scope.save = function () {
var AddSettings = $resource('../AddSettings/');--Calling my WCF REST SERVICE
var adminSettings = new AddSettings();
adminSettings.Title = $scope.inputtitle;
adminSettings.HomeUrl = $scope.intputhomeurl;
//var arr = [];
//arr.push(addmodule.Title);
//arr.push(addmodule.HomeUrl);
//addmodule.LogoSettings = arr;
adminSettings.$save();
It's generating following Json Object : { "UserName": "a","Password": "a"},Can any one please guide me to create a json Object like " {'Email': { 'UserName': 'a','Password': 'a'}}" using Angular js.
Thanks in advance
I solved my problem in the following way.
var adminSettings = new AddSettings();
adminSettings.LogoSettings = {
Title: $scope.inputtitle,
HomeUrl: $scope.intputhomeurl,
logo: $("#ImageUpload > img").attr('src')
};
adminSettings.$save();
It's returning Json object as {'Email': { 'UserName': 'a','Password': 'a'}}.

Add object to array in JSON

I am trying to add an object in an array to an item in a JSON object.
The result I am looking for is:
{ "AvailableFacets":[ "color", "sheenlevel" ],
"Selections":[
{ "Facet":"color", "Value":"red" },
{ "Facet":"color", "Value":"blue" }
]
}
but I get the error "TypeError: myJsonObject.Selection.push is not a function" when doing the following:
var testJson = function () {
var myJsonObject = $.parseJSON('{"AvailableFacets":["color", "sheenlevel"]}');
myJsonObject.Selection = "[]";
var newObject1 = $.parseJSON('{"Facet":"color", "Value":"red"}');
var newObject2 = $.parseJSON('{"Facet":"color", "Value":"blue"}');
myJsonObject.Selection.push(newObject1);
return myJsonObject;
};
What am I doing wrong?
"[]" !== []. Did that help? You are using the wrong types. Also you are looking for an output with "Selections" but you are attempting to define "Selection", but I assume that is a typo. This should work:
myJsonObject.Selection = [{"Facet":"color", "Value":"red"},{"Facet":"color", "Value":"blue"}];
But if you wanted to parse a string of JSON as JSON then just change
myJsonObject.Selection = "[]";
to:
myJsonObject.Selection = [];