Find values in a JSON string using VB.Net - json

I’m a novice and am struggling to work out how to use the Newtonsoft JSON methods to pull out of this JSON string what I’m after with VB.Net.
The first thing to note is that the JSON is in two parts, the Total and then the results.
"{""total"":9,""results"":[{""id"":1334,""description"":""Fiat"",""type"":0},{""id"":1331,""description"":""Ford"",""type"":0},{""id"":1330,""description"":""Nissan"",""type"":0},{""id"":1335,""description"":""Project
Alpha"",""type"":0},{""id"":1336,""description"":""Project
Beta"",""type"":0},{""id"":1337,""description"":""Project
Gamma"",""type"":0},{""id"":1333,""description"":""Tesla"",""type"":0},{""id"":1332,""description"":""Toyota"",""type"":0},{""id"":1329,""description"":""Volkswagen"",""type"":0}]}"
What I want to do is search the JSON for a particular description and get back the ID. For example if I searched for “Toyota” I would get back “1332”.
I have looked at the examples on this and other forums but failed to get any to work for me.
BTW I note that I could loop through the children in the ‘results’ but looping seems inefficient so I’m hoping to use a more efficient method.

First create an Object representing your Json (copy your json to clipboard, in Visual Studio create an empty class and paste it as json classes Edit -> Paste Spacial -> paste json as class - I have a german VS, so maybe the menu is named differently)
Deserialize the json string in a new instance of this class
do your filtering as you need
I made a sample app for you on GitHub

Related

How to convert a given xml to an instance of a defined Typescript class?

Hello,
In my application im going to use cxsd module in order to generate a Typescript class from a given xsd. The application is fed by xml inputs that I then do some logics on, converting the input to a known object by another web service and send it to the web service.
For example, recieving a DogXML input -> converting to OutputDog -> sending to the webservice that expects the OutputDog
I couldnt find converting the input xml to the output object so trivial.
Writing an application with Typescript made it clear for me that i need to somehow parse the xml input to the generated Typescript classes by the cxsd. my question is how?
How do you parse a given xml to an instance of an existing Class?
What i want to achieve is :
Recieving a DogXML
Creating a Dog instance of this xml
Do logic and convert it to OutputDog
Send the OutputDog to the web service
Currently i am using xml2js module to convert the input xml into a JSON and work on it, the problem is that the output of the parsing to JSON is any and I cant access the properties of Dog since it doesn't know Dog's properties. This is why I need to create an instance of Dog from the given XML
Thanks in advance, hopefully i made myself clear!

XML To JSON and Vice Versa

Team,
This has been a problem in apps-script development for me. I have been trying to solve it for quite some time.
The XML appscript service I have been using (now deprecated) has been doing excellent, I mean really awesome JOB of parsing the any html file or XML file with the linient flag turned of. I am in love with it.
Also, this service gives you a JSON view of XML file. that is really cool. because I can simply browse to any data item in the debugger, and then type that in code. No need to call many sensless methods when you know exactly what your code is looking for.
These AWESOME features in deprecated XML service make the job a lot easier than the so called XMLService which it replaces.
One issue, I did find with XML service is it is slow. I wrote a parser that does it within 10 seconds that XML Service takes 3 minutes. So, sure it is bad.
But, XMLService does not have those neat features.
IS there any public code or libraries by anyone that does a beautiful job of:
a. Converting XML to JSON view.
b. Parse HTML in linient form.
c. Do it fast
Please help.
Ok, I didn't receive any update on the matter. So, I have written it myself. I simply convert XML to JSON.
Xml2Json will represent an XML string as a JSON object.
If you set flags="attributes", then the element attributes are packaged in XmlAttributes child of the JSONobject.
In XML, there is no way to say, you are about to encounter an array of elements or a single element. So, the parser by default creates a JSON object out of an element unless it counters another xml element with same name, then it converts the Object into an Array.
Here is the Xml2Json Project Key: 18Aji4ggm4A2cXQ1n_sSc6gDQA3Wc4aC5WvQYybAflkdUushu9f2Ogv5o

VBJSON for VB6 how to serialize object returned from Parse routine

So there is a nice library for VB6 JSON parsing. HERE
but i actually used one that built on the original and optimized. HERE
Essentially I'm using the parser to deserialize the json i get from a web service. I need to update some values, and resend to the server. Using the Collection/Dictionary objects made it very easy. But now, How do i take those objects and serialize them to a JSON string? is there a library for that?
thanks you for your help.
There are quite a few JSON parser/serializer/DOM classes written in VB6. Perhaps you might want to consider one of those instead. E.g.:
JsonBag, Another JSON Parser/Generator

Parsing JSON in VBA, without any external library

I'm really at my wit's end here... I'm using VB-JSON Parser (http://www.ediy.co.nz/vbjson-json-parser-library-in-vb6-xidc55680.html) and I have the following array :
[{"timestamp":1410001952,"tid":2834225,"price":"483.77"}]
The documentation is really minimal and I have no clue whatsoever of how to access the array, been searching for several hours now on how to resolve this.
How can I get the "price" value? I know that i can use .item("price") when there is no array but I don't know what to do when there's an array and there is no name before it.
First have a look at Parsing JSON in Excel VBA
It explains the JScript way of parsing JSON string.
Browsing through the net, I found it really hard to get a complete VBA based JSON parser.
Some options are available in the VB version and then there are few online parsers who promise to parse JSON and convert them in Excel. These ones work fine with simple JSON data structure. But once you feed in a complex data set with nested arrays and structures, they simply fail.
Using JavaScript features of parsing JSON, on top of ScriptControl, we can create a parser in VBA which will list each and every data point inside the JSON. No matter how nested or complex the data structure is, as long as we provide a valid JSON, this parser will return a complete tree structure.
JavaScript’s Eval, getKeys and getProperty methods provide building blocks for validating and reading JSON.
Coupled with a recursive function in VBA we can iterate through all the keys (up to nth level) in a JSON string. Then using a Tree control (used in this article) or a dictionary or even on a simple worksheet, we can arrange the JSON data as required.
Here, you can find a complete VBA example.
There is a JSON serializer in .NET: http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json

Javascript in place of json input step

I am loading data from a mongodb collection to a mysql table through Kettle transformation.
First I extract them using MongodbInput and then I use json input step.
But since json input step has very low performance, I wanted to replace it with a
javacript script.
I am a beginner in Javascript and even though i tried somethings, the kettle javascript script is not recognizing any keywords.
can anyone give me sample code to convert Json data to different columns using javascript?
To solve your problem you need to see three aspects:
Reading from MongoDB
Reading from JSON
Reading from (probably) String
Reading from MongoDB Except if you changed the interface, MongoDB returns not JSON but BSON files (~binary JSON). You need to see the MongoDB documentation about reading and writing BSON: probably something like BSON.to() and BSON.from() but I don't know it by heart.
Reading from JSON Once you have your BSON in JSON format, you can read it using JSON.stringify() which returns a String.
Reading from (probably) String If you want to use the capabilities of JSON (why else would you use JSON?), you also want to use JSON.parse() which returns a JSON object.
My experience is that to send a JSON object from one step to the other, using a String is not a bad idea, i.e. at the end of a JavaScript step, you write your JSON object to a String and at the beginning of the next JavaScript step (can be further down the stream) you parse it back to JSON to work with it.
I hope this answers your question.
PS: writing JavaScript steps requires you to learn JavaScript. You don't have to be a master, but the basics are required. There is no way around it.
you could use the json input step to get the values of this json and put in common rows