how to get individual valued from the json array..? - json

I got json array as output. could anyone explain me how to get indivual value from it for eg( C:/). The following is the json array
{"Name":["C:\/","D:\/","E:\/","F:\/","G:\/","My Documents","Microsoft Outlook","Microsoft Outlook Express","Opera","Mozilla","Internet Explorer Settings","FireFox","Desktop","Registry","SystemState"],"path":["C:\/","D:\/","E:\/","F:\/","G:\/","\/\/\/My Documents\/\/\/","\/\/\/Microsoft Outlook\/\/\/","\/\/\/Microsoft Outlook Express\/\/\/","\/\/\/Opera\/\/\/","\/\/\/Mozilla\/\/\/","\/\/\/Internet Explorer Settings\/\/\/","\/\/\/FireFox\/\/\/","\/\/\/Desktop\/\/\/","\/\/\/Registry\/\/\/","\/\/\/SystemState\/\/\/"],"hasChild":["1","1","1","1","1","0","0","0","0","0","0","0","0","0","0"]}

Assuming your language is JavaScript, you first need to parse the JSON (below I use jQuery's $.parseJSON method) and then access the appropriate property and array position. To retreive "C:\", you would do this:
var sJSON = '{"Name":["C:\/","D:\/","E:\/","F:\/","G:\/","My Documents"]}';
var oJSON = $.parseJSON(sJSON);
alert(oJSON.Name[0]); //displays C:\
If you're using JavaScript, you can use an eval() statement in place of a $.parseJSON because JSON is executable in JavaScript, but it is safer to use a $.parseJSON.
More on JavaScript eval(): When is JavaScript's eval() not evil?

Use a JSON parsing library for your language.
In JavaScript, you can use JSON.parse. If your output is in the var output, you would do:
var obj = JSON.parse(output);
console.log(obj.path[0]);

Related

selecting a key-value from API response

I have this response from an API:
payment_object = {'acquirer_response': '{"object":"transaction", "pix_expiration_date":"2022-04-28T13:46:01.000Z"}'}
how can i select the pix_expiration_date key??? I have tried:
payment_object['acquirer_response']['pix_expiration_date']
it doesnt work and it returns to me:
TypeError: string indices must be integers
Since you’re looking for json
Here’s a code you can start with, keep the good work :)
import json
#importing json
payment_object = {'acquirer_response': '{"object":"transaction", "pix_expiration_date":"2022-04-28T13:46:01.000Z"}'}
###
dic=json.loads(payment_object["acquirer_response"])
#loading the object
dic['pix_expiration_date']
#it will return 2022-04-28T13:46:01.000Z
Are you the owner of the api? If yes:
Edit the response, because it’s sending an string not an object/dictionary
If No:
Convert the string to an dictionary, like if you using python try json library
Your json is not valid. I don't know what language are you using, this code works for javascript, you can easily translate it to another language
var payment_object = {'acquirer_response': '{"object":"transaction", "pix_expiration_date":"2022-04-28T13:46:01.000Z"}'};
var s=JSON.stringify(payment_object).replaceAll("\\","").replaceAll("\"{","{").replaceAll("}\"","}");
var paymentObject=JSON.parse(s);
console.log(paymentObject['acquirer_response']['pix_expiration_date']); // 2022-04-28T13:46:01.000Z

Can you use 'require' in react to import a library?

In my react project, I'm trying to convert XML data from an API call into JSON (using a library called xml-js).
As per the documentation, I'm importing the library in my parent component as follows
const convert = require('xml-js')
and then attempting the convert the API data as follows
const beerList =
'<Product>
<Name>Island Life IPA</Name>
<Volume>300ml/473ml</Volume>
<Price>$10/$13</Price>
<ABV>6.3%</ABV>
<Handpump>No</Handpump>
<Brewery>Eddyline</Brewery>
<IBU/>
<ABV>6.3%</ABV>
<Image>islandlife.png</Image>
<Country>New Zealand</Country>
<Description>Fruited IPA</Description>
<Pouring>Next</Pouring>
<IBU/>
<TapBadge/>
<Comments/>
</Product>'
const beerJs = convert(beerList,{compact: true, spaces: 4})
The errors are telling me that 'convert' is not a function, which tells me that the library isn't being imported. So is the issue with using 'require' syntax, and if so, what alternative would work in react?
which tells me that the library isn't imported
No. If that were the case, you wouldn't even get that far, your require call would throw an error.
Instead, it tells you that convert is not a function - which it isn't! Look at it in a debugger or log it, and you'll see it's an object with several functions inside. You can't call an object like a function.
Take a look at the xml-js docs again:
This library provides 4 functions: js2xml(), json2xml(), xml2js(), and xml2json(). Here are the usages for each one (see more details in the following sections):
var convert = require('xml-js');
result = convert.js2xml(js, options); // to convert javascript object to xml text
result = convert.json2xml(json, options); // to convert json text to xml text
result = convert.xml2js(xml, options); // to convert xml text to javascript object
result = convert.xml2json(xml, options); // to convert xml text to json text
So the solution is to call convert.xml2json and not convert:
const beerJs = convert.xml2json(beerList, {compact: true, spaces: 4})
Or maybe you want an actual object and not a JSON string, then you'd use convert.xml2js (in which case the spaces option is useless):
const beerJs = convert.xml2js(beerList, {compact: true})

QBO3 XML to JSON converter

I would like to convert XML data to a JSON object within QBO3.
Is there a native method available for this?
Try running the following in console:
var xml = "<head><node1>node1</node1><node2>node2</node2></head>"
var xmlToJson = new XMLtoJSON;
xmlToJson.fromStr(xml);
Note that in addition to the fromStr[ing] method, the XMLtoJSON object also has a fromFile method available.

Is this a correct JSON? How we can parse it?

My web service is returning below mentioned output as JSON,
{"FetchSitePerformanceAutoResult":[{"DailyTimeStamp":"Nov 01, 2013","Performance":106917}]}
But I doubt cause I am not able to parse it.
If it is correct pls tell me how to parse it? I want to bind it to chart.
Full code
var retVal = '{"FetchSitePerformanceAutoResult":[{"DailyTimeStamp":"Nov 01, 2013","Performance":106917},{"DailyTimeStamp":"Nov 02, 2013","Performance":119542}]}';
alert('Before parsing ' + retVal);
var passValue = JSON.parse(retVal);
alert('After parsing Count ' + passValue.count);
last alert give output as After parsing Count undefined.
The simplest way to parse your json in javascript is that you store it inside a variable and can proceed like this:
var jsonString=JSON.parse(yourJsonString);
then you can fetch each key and their value accordingly.
Just check your json on JsonEditoronline.org for a better way of reading this.
This is sample of how I read my json in javascript:
success:function(responseText)
{
alert(responseText);
return false;
var object= JSON.parse(responseText);
var response= object.response;
var div=$('<div></div>');
var docs= response.docs;
where my json was something like this:
{"response":{"docs":""}}
Hope it helps.
I parsed your json using javascript and was able to parse it using :
var myString='{"FetchSitePerformanceAutoResult":[{"DailyTimeStamp":"Nov 01, 2013","Performance":106917}]}';
var jsonString=JSON.parse(myString);
alert("---------==="+jsonString);
var fetch= jsonString.FetchSitePerformanceAutoResult;
alert("llllllll -- "+fetch.length);
var time= fetch[0].DailyTimeStamp;
alert(time);
To tell if JSON is correct, you can use http://jsonlint.com/ Your JSON is correct. To parse it, use the .net JSON Deserializer http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx or JSON.NET Also see here for some discussion: Does .NET 4 have a built-in JSON serializer/deserializer?
EDIT: as you want Javascript, use JSON.parse. See some discussion here: http://www.json.org/js.html which also explains why you should not use eval(json), which does also work, but is considered a security risk.
Yes your json output is correct and you can check yourself here is the Link, this will help you convert your json to c# class. Hope this helps.
That is a valid JSON String. You can check it at http://jsonlint.com/
It is legal - php json_decode('{"FetchSitePerformanceAutoResult":[{"DailyTimeStamp":"Nov 01, 2013","Performance":106917}]}'); parses it fine.

Could not retrieve json data from the given format

this is my json format
({"message":{"success":true,"result":[{"lead_no":"LEA13","lastname":"Developer","firstname":"PHP","company":"Dummies","email":"nandhajj#gmail.com","id":"10x132"},{"lead_no":"LEA14","lastname":"Venu","firstname":"Yatagiri","company":"Rsalesarm","email":"veve#jajs.com","id":"10x133"},{"lead_no":"LEA4","lastname":"Jones","firstname":"Barbara","company":"Vtigercrm inc","email":"barbara_jones#company.com","id":"10x35"},{"lead_no":"LEA1","lastname":"Smith","firstname":"Mary","company":"Vtiger","email":"mary_smith#company.com","id":"10x32"}]}})
i am trying to retrieve the whole json result values using the following snippet
if (xmlHttp.readyState==4)
{
alert(xmlHttp.status);
if(xmlHttp.status==200)
{
alert("hi");
var jsondata=eval("("+xmlHttp.responseText+")") //retrieve result as an JavaScript object
jsonOutput=jsondata.result;
alert(jsonOutput);
InitializeLeadStorage()
}
}
my alert (hi ) is displayed but the alert(jsonOutput); is undefined , please help me if you could find any mistake
jsonOutput = jsondata.message.result;
result lives on message - it is not a top-level item in the JSON. With things like this, console.log() the JSON and you can check the path to the bit you want.
Also
your variable is global
there are better ways of parsing your JSON. If you don't care about old IEs, you can use the ECMA5 JSON.parse(), else use jQuery or another third-party utility for this