?
Solved

JSON - How to search and replace values

Posted on 2015-01-04
2
Medium Priority
?
196 Views
Last Modified: 2015-01-04
How can I search and replace values in JSON?
I want to replace the values in the 'docs' with the values in the 'highlighting'.  So I can bind the highlighted with the <em> tag.

Any ideas?
thanks




            var data = [
                {
                    "responseHeader":{
                        "status":0,
                        "QTime":10,
                        "params":{
                            "wt":"json",
                            "facet":"true",
                            "hl.fl":"name,features",
                            "facet.mincount":"1",
                            "facet.field":"cat",
                            "indent":"on",
                            "hl":"true",
                            "q":"video"}},
                    "response":{"numFound":3,"start":0,"docs":[
                           {
                               "id":"MA147LL/A",
                               "sku":"MA147LL/A",
                               "name":"Apple 60 GB iPod with Video Playback Black",
                               "manu":"Apple Computer Inc.",
                               "includes":"earbud headphones, USB cable",
                               "weight":5.5,
                               "price":399.0,
                               "popularity":10,
                               "inStock":true,
                               "timestamp":"2007-01-31T05:12:44.562Z",
                               "cat":[
                                "electronics",
                                "music"],
                               "features":[
                                "iTunes, Podcasts, Audiobooks",
                                "Stores up to 15,000 songs, 25,000 photos, or 150 hours of video",
                                "2.5-inch, 320x240 color TFT LCD display with LED backlight",
                                "Up to 20 hours of battery life",
                                "Plays AAC, MP3, WAV, AIFF, Audible, Apple Lossless, H.264 video",
                                "Notes, Calendar, Phone book, Hold button, Date display, Photo wallet, Built-in games, JPEG photo playback, Upgradeable firmware, USB 2.0 compatibility, Playback speed control, Rechargeable capability, Battery level indication"]},
                           {
                               "id":"EN7800GTX/2DHTV/256M",
                               "sku":"EN7800GTX/2DHTV/256M",
                               "name":"ASUS Extreme N7800GTX/2DHTV (256 MB)",
                               "manu":"ASUS Computer Inc.",
                               "weight":16.0,
                               "price":479.95,
                               "popularity":7,
                               "inStock":false,
                               "timestamp":"2007-01-31T05:12:45.187Z",
                               "cat":[
                                "electronics",
                                "graphics card"],
                               "features":[
                                "NVIDIA GeForce 7800 GTX GPU/VPU clocked at 486MHz",
                                "256MB GDDR3 Memory clocked at 1.35GHz",
                                "PCI Express x16",
                                "Dual DVI connectors, HDTV out, video input",
                                "OpenGL 2.0, DirectX 9.0"]},
                           {
                               "id":"100-435805",
                               "sku":"100-435805",
                               "name":"ATI Radeon X1900 XTX 512 MB PCIE Video Card",
                               "manu":"ATI Technologies",
                               "weight":48.0,
                               "price":649.99,
                               "popularity":7,
                               "inStock":false,
                               "timestamp":"2007-01-31T05:12:45.203Z",
                               "cat":[
                                "electronics",
                                "graphics card"],
                               "features":[
                                "ATI RADEON X1900 GPU/VPU clocked at 650MHz",
                                "512MB GDDR3 SDRAM clocked at 1.55GHz",
                                "PCI Express x16",
                                "dual DVI, HDTV, svideo, composite out",
                                "OpenGL 2.0, DirectX 9.0"]}]
                    },
                    "facet_counts":{
                        "facet_queries":{},
                        "facet_fields":{
                            "cat":[
                             "electronics",3,
                             "card",2,
                             "graphics",2,
                             "music",1]}},
                    "highlighting":{
                        "MA147LL/A":{
                            "name":["Apple 60 GB iPod with <em>Video</em> Playback Black"],
                            "features":["Stores up to 15,000 songs, 25,000 photos, or 150 hours of <em>video</em>"]},
                        "EN7800GTX/2DHTV/256M":{
                            "features":["Dual DVI connectors, HDTV out, <em>video</em> input"]},
                        "100-435805":{
                            "name":["ATI Radeon X1900 XTX 512 MB PCIE <em>Video</em> Card"]}}          
                }];
0
Comment
Question by:JElster
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 2000 total points
ID: 40530475
Maybe something like this:
  $.each(data, function(idx1, obj1) {
    $.each(obj1.highlighting, function(idx2, obj2) {
      $.each(obj1.response.docs, function(idx3, obj3) {
        if (obj3.id == idx2) {
          $.each(obj2, function(idx4, obj4) {
            if (idx4 in obj3) {
              obj3[idx4] = obj2[idx4];
            }
          });
        }
      });
    });
  });

Open in new window

I also made a jsfiddle to show the results.
0
 
LVL 1

Author Closing Comment

by:JElster
ID: 40530595
Awesome!  again!
thx
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question