MuggerBugger
MuggerBugger

Reputation: 7

Parse specific data using JSON data

Trying to find the "age" based on the "name" of the below XML data.

    <data>
        <user>
            <name>Joe</name>
            <age>34</age>
        </user>
        <user>
            <name>Jimmy</name>
            <age>26</age>
        </user>
    </data>

I used xml2json to parse this, returning

{"data":{"user":[{"name":"Joe","age":"34"},{"name":"Jimmy","age":"26"}]}}

With this, how would I be able to get the value 34 by entering in "Joe"?

Upvotes: 0

Views: 95

Answers (1)

James
James

Reputation: 82096

Parse the string, then find the record

const json = JSON.parse("...");
const joe = json.data.user.find(x => x.name === "Joe");
console.log(joe.age);

Upvotes: 1

Related Questions